Ask your JavaScript questions! Pay money and get answers fast! (more info)

jQuery.ajax() never fires, no matter what I do JavaScript

  • SOLVED

This is driving me crazy. I have a link like this:

<span><a href="#" onclick="rateThisAdWithStars(1,<?php echo $sf_user->getGuardUser()->id ?>,<?php echo $ad->id ?>); return false;"><img src="/img/knewstuff.png" alt="Rate this ad" style="height: 15px; width: 15px;" /></a> </span>


And this function in my Javascript file:

function rateThisAdWithStars(howManyStars, userId, adId) {

alert(howManyStars + ' ' + userId + ' ' + adId);
jQuery.ajax({
type: "GET",
url: "public.php/ad/rate",
success: function(data) {
alert(data.responseText);
},
data: { user_id: userId,
ad_id: adId,
rating: howManyStars }
});
return false;
}



I open up FireFox, and I open FireBug. In FireBug, I look in the console to see the GET request fire.

When I click the link in my browser I see that first alert() and all of the number are correct. And then nothing happens. The GET request never fires. In the FireBug console, there is no error, there is no nothing. It is as if the function stops executing at that point.

I also tried writing it as:

$.ajax({

and I tried make the path absolute:

url: "/public.php/ad/rate",

The ajax function does not fire. What am I missing?

I import jQuery onto the page at the top of my template with this:


<script type="text/javascript" src="/js/jquery-1.7.2.min.js"></script>




UPDATE:

Tested on Chrome and got this:

Failed to load resource: the server responded with a status of 404 (Not Found)
http://lips2.example.com/public_dev.php/ad/show/id/[object%20Object]



I try this:



function rateThisAdWithStars(howManyStars, userId, adId) {
jQuery.ajax({
url: "/public.php/ad/rate",
data: { user_id: userId,ad_id: adId,rating: howManyStars },
success: function(data) {
alert(data.responseText);
},
failure: function() { alert(xhr.status and xhr.responseText); }
error: function() { alert(xhr.status and xhr.responseText); }
});
}



And this line triggers an error;


jQuery.ajax({

I get:

Unexpected token "."

What the hell?

UPDATE:

I do this:




function rateThisAdWithStars(howManyStars, userId, adId) {
jQuery.ajax({
url: "/public_dev.php/ad/rate",
data: { user_id: userId,ad_id: adId,rating: howManyStars },
success: function(data) {
alert(data.responseText);
}
// failure: function() { alert(xhr.status and xhr.responseText); }
// error: function() { alert(xhr.status and xhr.responseText); }
});
}




This seems to work in Chrome but not in FireFox. And even in Chrome, the alert() shows me a value of Undefined.


UPDATE:

Okay, now working in Chrome correctly, but still not working in FireFox. This is what works in Chrome:



function rateThisAdWithStars(howManyStars, userId, adId) {
var currentjqXHR = jQuery.ajax({
url: "/public_dev.php/ad/rate",
data: { user_id: userId,ad_id: adId,rating: howManyStars },
success: function(data) {
// alert(data.responseText);
}
// failure: function() { alert(xhr.status and xhr.responseText); }
// error: function() { alert(xhr.status and xhr.responseText); }
});
currentjqXHR.success(function() { alert(currentjqXHR.responseText); });
}



jquery 1.7

UPDATE:

Hmm, okay, its now working for my co-worker, in both FireFox and Chrome. Still not working for me in FireFox, but I will assume it has something to do with my personal settings, or something on my machine.

I'll wrap this one up.

Answers (5)

2012-08-23

Ross Wilson answers:

Does it execute after you click OK on the alert? Does it hit a breakpoint if you set one in fire bug?


Lawrence Krubner comments:

No, nothing happens. The alert box shows up, I click okay, and then nothing happens. At the Javascript console in FireBug, no Ajax call ever seems to occur. And that is the next line of code, so I'm not sure where I should set a break point.


Lawrence Krubner comments:

Set a breakpoint on every line. The code seems to walk through, but nothing happens. (I'm disguising the dev URL in the image, but not in my actual run)