Reputation: 12391
I have the following js code on a button click:
$("#processCategories").click(function () {
console.log('HERE');
$.ajax({
url: ajaxUrl, // point to server-side PHP script
data: {action: 'processCategories'},
type: 'post',
done: function () {
console.log('SUCCESS');
//location.reload();
},
fail: function (msg) {
console.log('FAIL');
},
always: function (msg) {
console.log('ALWAYS');
}
});
});
In my php file, I've added the next line:
header('HTTP/1.0 404 Not found');
die();
I've also tried it with forbidden.
So, console logs: HERE
, but not FAIL
and neither the ALWAYS
I am using this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
The documentation says:
Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
If I rewrite these to to error
and complete
, both of them are running.
What do I miss?
UPDATE
.done
is not runs also. success
does.
Upvotes: 2
Views: 3297
Reputation: 207511
They follow the pattern of jQuery's promises so you need to chain them to the Ajax call.
$.ajax({
url: ajaxUrl, // point to server-side PHP script
data: {action: 'processCategories'},
type: 'post'
}).done(function () {
console.log('SUCCESS');
//location.reload();
}).fail(function (msg) {
console.log('FAIL');
}).always(function (msg) {
console.log('ALWAYS');
});
Upvotes: 4
Reputation: 943578
You are confusing jqXHR methods with $.ajax options.
$.ajax
will return a jqXHR object and that will have a fail method, so you could do:
$.ajax({
url: ajaxUrl, // point to server-side PHP script
data: {action: 'processCategories'},
type: 'post'
}).fail(function (msg) {
console.log('FAIL');
});
$.ajax
options which take functions for handling success and error conditions are:
jqXHR methods which takes functions for the same are:
Upvotes: 9