来自php的$ .ajax(延迟)对象强制失败

Nic*_*ick 6 ajax jquery

我正在做一个小的ajax乒乓,并想知道是否有可能强制延迟的ajax对象从PHP进入失败状态.

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

在PHP中

function post() {
if (false) {
    echo json_encode(array('all good'));
} else {
    ???
}
}
Run Code Online (Sandbox Code Playgroud)

Ja͢*_*͢ck 5

您可以返回错误标头:

header('HTTP/1.0 404 Not found');
exit;
Run Code Online (Sandbox Code Playgroud)

这将导致jQuery运行其错误处理程序,从而导致Ajax延迟失败.基本上像4xx5xx这样的状态代码就可以了.