Here is a dirt-simple AJAX request with anonymous callbacks. Both plete
and error
are firing. Why?
$.ajax({
url:'/echo/js/?js=hello%20world!',
plete: function (response) {
console.log("In anonymous success callback");
console.log("response text: " + response.responseText);
console.log("response object:");
console.log(response);
},
error: function (error) {
console.log("In anonymous error callback");
console.log("error object:");
console.log(error);
}
});
/
EDIT I tried using promises, and now I'm only getting error. Something must be wrong with my "dirt-simple" ajax.
$.ajax({
url: '/echo/js/?js=hello%20world!',
type: 'POST',
})
.done(function(response) {
console.log("In anonymous JS success callback");
console.log("response text: " + response.responseText);
console.log("response object:");
console.log(response);
})
.fail(function(error) {
console.log("In anonymous JS error callback");
console.log("error object:");
console.log(error);
})
.always(function(data) {
console.log("JS I will always do this");
console.log(data);
});
/
Here is a dirt-simple AJAX request with anonymous callbacks. Both plete
and error
are firing. Why?
$.ajax({
url:'/echo/js/?js=hello%20world!',
plete: function (response) {
console.log("In anonymous success callback");
console.log("response text: " + response.responseText);
console.log("response object:");
console.log(response);
},
error: function (error) {
console.log("In anonymous error callback");
console.log("error object:");
console.log(error);
}
});
https://jsfiddle/abalter/tz0tu04y/
EDIT I tried using promises, and now I'm only getting error. Something must be wrong with my "dirt-simple" ajax.
$.ajax({
url: '/echo/js/?js=hello%20world!',
type: 'POST',
})
.done(function(response) {
console.log("In anonymous JS success callback");
console.log("response text: " + response.responseText);
console.log("response object:");
console.log(response);
})
.fail(function(error) {
console.log("In anonymous JS error callback");
console.log("error object:");
console.log(error);
})
.always(function(data) {
console.log("JS I will always do this");
console.log(data);
});
https://jsfiddle/abalter/bjwc1dx1/
Share Improve this question edited Apr 21, 2016 at 17:56 abalter asked Apr 21, 2016 at 17:25 abalterabalter 10.4k18 gold badges98 silver badges167 bronze badges 4- 2 use success and error callbacks instead. – Nikhilesh K V Commented Apr 21, 2016 at 17:31
- 1 RTFM. – Oka Commented Apr 21, 2016 at 17:32
- Ok. I RTFM, and used promises as @escaparello suggested. Still getting an error. Updated question. – abalter Commented Apr 21, 2016 at 17:55
- @abalter Check my answer for the solution to your second question – dlsso Commented May 11, 2016 at 17:00
3 Answers
Reset to default 7From the jQuery's ajax() documentation:
plete
A function to be called when the request finishes (after success and error callbacks are executed)
Now with modern versions of jQuery the preferred method to listen for success and error would be to use promise interface.
$.ajax(...)
.done( function() {}) //success
.fail( function() {}) //error
.always( function() {}); //plete
Complete is not the same as success. It fires when the response es back regardless of the status.
Update to answer your second question:
The reason you are getting the error in the first part of the second fiddle is that it is expecting javascript and you gave text that is not valid javascript.
If you send it to an endpoint that your format is valid for, or put your hello world in quotes (make it valid javascript) it will work.
When you configure an AJAX request you can set several options, among them you define the callbacks for the beforeSend, success, error and plete events. If you check this http://api.jquery./jquery.ajax/ you will see that the plete callback is invoked for both, success and error events. Cheers