Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ajax状态代码的jQuery默认值_Jquery_Default_Http Status Codes - Fatal编程技术网

ajax状态代码的jQuery默认值

ajax状态代码的jQuery默认值,jquery,default,http-status-codes,Jquery,Default,Http Status Codes,据我所知,使用ajax处理错误结果的一种可能性如下: $.ajax({ url: someUrl, type: 'POST', success: function(data) {}, error: function(jqXHR, exception) { if (jqXHR.status === 0) { alert('Not connect.\n Verify Network.'); } else if (

据我所知,使用ajax处理错误结果的一种可能性如下:

$.ajax({
    url: someUrl,
    type: 'POST',
    success: function(data) {},
    error: function(jqXHR, exception) {
        if (jqXHR.status === 0) {
            alert('Not connect.\n Verify Network.');
        } else if (jqXHR.status == 404) {
            alert('Requested page not found. [404]');
        } else if (jqXHR.status == 500) {
            alert('Internal Server Error [500].');
        } else if (exception === 'parsererror') {
            alert('Requested JSON parse failed.');
        } else if (exception === 'timeout') {
            alert('Time out error.');
        } else if (exception === 'abort') {
            alert('Ajax request aborted.');
        } else {
            alert('Uncaught Error.\n' + jqXHR.responseText);
        }
    }
});
或者使用
statusCode
使其更具可读性:

$.ajax({
    url: someUrl,
    type: 'POST',
    statusCode: {
        200: function(data) {
                   :
                   :
             },
        401: function() {
                   :
                   :
             },
              :
              :
我的问题是:

是否可以使用
statusCode
并为其设置默认的故障诊断码?

不在statusCode参数中。但这是因为如果你想抓住一切,有一种更好(更精简)的方法:

complete: function(jqXHR, textStatus) {
    switch (jqXHR.status) {
        case 200:
            alert("200 received! yay!");
            break;
        case 404:
            alert("404 received! boo!");
            break;
        default:
            alert("I don't know what I just got but it ain't good!");
    }
}

太好了,谢谢!非常干净和简单。对于那些要使用它的人,我想补充一点,为了从请求中获取json,您需要使用
data=jQuery.parseJSON(jqXHR.responseText)