I’m trying to send an ajax request using jquery. From the tutorial, I try this simple script:
function show(){
$("#mesg").html("begin");
var request = $.ajax({
type: "POST",
url: "some url",
data: '{"user_name": "NAME"}',
contentType: "application/json; charset=utf-8",
dataType: "json"
});
$("#mesg").html("waiting");
request.done(function(){
$("#mesg").html("done sending");
});
$("#mesg").html("still waiting");
request.fail(function(){});
$("#mesg").html("end of function");
}
So the request is supposed to fail and the content of #mesg should be “end of function”. However, what I got was “waiting,” which is before request.done(). So is there a syntax error here?
If you are using a version of jQuery earlier than 1.5
.ajax()doesn’t support the promise interface so I would think your script would crash when it tries to use.done()and that would be consistent with processing stopping at the “waiting” message.Update to the latest version (or at least 1.5+) and it should work.