Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax callback never called

Javascript code, using jQuery 1.7:

$( function() { 
  $.get('/ajax_dummy', function() { alert('foo');  })
});

With Firebug I can see that the HTTP GET request is sent and a "hello world" response with code 200 is returned, so everything seems fine. But the callback is never called.

I have no idea what is wrong; this should be so simple, right?

like image 364
Lucy Brennan Avatar asked Dec 14 '11 18:12

Lucy Brennan


2 Answers

You are not providing dataType so jQuery makes an "intelligent guess" of what the content type is from the response Content-Type header which you said is application/json.

So jQuery treats the response as JSON which means it will try to automagically parse it as so, causing an error.

Because the request causes an error

$.parseJSON( "hello world" );
"Invalid JSON: hello world"

the success callback won't obviously be fired.

like image 141
Esailija Avatar answered Nov 09 '22 05:11

Esailija


Give this a rip:

$.ajax("/ajax_dummy", {
    dataType: "text",
    success: function() {
        console.log("winning.");
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus); //error logging
    }
});
like image 43
Naftuli Kay Avatar answered Nov 09 '22 04:11

Naftuli Kay