2016-09-29 57 views

回答

2

状态代码500是其在error处理程序的处理ajaxInternal Server Error的指示器。

jQuery.post(url [, data ] [, success ] [, dataType ])

参考下面这个例子:

var jqxhr = $.post("example.php", function() { 
    alert("success"); 
}) 
    .done(function() { 
    alert("second success"); 
    }) 
    .fail(function() { 
    alert("error"); 
    }) 
    .always(function() { 
    alert("finished"); 
}); 

更新代码:

$.post("/signup", userData, function(data) { 
    console.log(data); 
}).fail(function(error) { 
    if (error.responseText == 'showAlert') { 
    alert("User already exists"); 
    } 
}); 
0

Here是状态代码的意思参考。

错误4XX,5XX

的4XX代码用于在客户端似乎有 犯错的情况下,对于案件的5xx的代码,该服务器知道 服务器已经犯错。一般来说这些情况是不可能区分的,所以差异只是信息性的。

因此,您应该单独处理错误响应。

$.post("/signup", userData, function(data) { 
    console.log(data); // executed if status code starts from 2XX 
}).error(function(error) { // executed if error i.e status code 4xx or 5xx 
if (error.responseText == 'showAlert') { 
    alert("User already exists"); 
} 
});