Force crash for $ .ajax (deferred) object from php

I am doing a little ajax ping-pong and wondering if it is possible to make a pending ajax object fall into a failure state from PHP.

$.ajax({ url: 'example.com/post', dataType: 'json' }) .done(function(data) { console.log(data); }) .fail(function(data) { console.log(data); }); 

and in php

 function post() { if (false) { echo json_encode(array('all good')); } else { ??? } } 
+6
source share
1 answer

You can return the error header:

 header('HTTP/1.0 404 Not found'); exit; 

This will cause jQuery to start the error handler and, in turn, not migrate Ajax. Basically, status codes like 4xx and 5xx` will do the trick.

+5
source

Source: https://habr.com/ru/post/916919/


All Articles