Force fail for $.ajax (deferred)object from php
ajax, jquery
Solution
You can return an error header:
header('HTTP/1.0 404 Not found');
exit;
This will cause jQuery to run its error handler and in turn fail the Ajax deferred. Basically status codes like `4xx` and 5xx` will do the trick.
Problem
i am doing a little ajax ping pong and was wondering if it is possible to force a deferred ajax object to fall into fail 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 { ??? } } ```