我通过这种方式设置了全局AJAX错误处理功能,但我不知道如何测试它?无论如何在jsFiddle
$(function () {
var statusErrorMap = {
'400' : "Server understood the request, but request content was invalid.",
'401' : "Unauthorized access.",
'403' : "Forbidden resource can't be accessed.",
'500' : "Internal server error.",
'503' : "Service unavailable."
};
//setup ajax error handling
$.ajaxSetup({
error: function (x, status, error) {
if (x.status in statusErrorMap) {
if (x.status == "503") {
alert("Sorry, your session has expired. Please login again to continue");
window.location.href ="/users/sign_in";
} else {
alert("("+ x.status +") error occurred: " + statusErrorMap[x.status.toString()]);
}
}
else {
alert("An error occurred: " + status + "nError: " + error);
}
}
});
});
答案 0 :(得分:0)
您必须使用fiddler(代理工具)拦截服务器请求并测试您的错误处理代码。 为此,您必须在fiddler中为服务调用设置断点,并按照以下过程进行操作。
您可以在下面的stackoverflow中获取additioanl详细信息。
How do I use Fiddler to modify the status code in an HTTP response?