jQuery ajax 错误函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6792878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ajax error function
提问by Darryl Wilson
I have an ajax call passing data to a page which then returns a value.
我有一个 ajax 调用将数据传递到一个页面,然后返回一个值。
I have retrieved the successful call from the page but i have coded it so that it raises an error in the asp. How do i retrieve that error from the jquery?
我已经从页面中检索到成功的调用,但是我已经对其进行了编码,因此它会在 asp 中引发错误。我如何从 jquery 中检索该错误?
For example:
例如:
cache: false,
url: "addInterview_Code.asp",
type: "POST",
datatype: "text",
data: strData,
success: function (html) {
alert('successful : ' + html);
$("#result").html("Successful");
},
error: function (error) {
**alert('error; ' + eval(error));**
}
It's the error bit that I don't understand. In the function what parameter do I need to put, so that I can then use the error message that I raisedin the server.
这是我不明白的错误位。在函数中我需要放置什么参数,以便我可以使用我在服务器中引发的错误消息。
回答by pala?н
The required parameters in an Ajax error
function are jqXHR, exception
and you can use it like below:
Ajaxerror
函数中所需的参数是jqXHR, exception
,您可以像下面这样使用它:
$.ajax({
url: 'some_unknown_page.html',
success: function (response) {
$('#post').html(response.responseText);
},
error: function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
$('#post').html(msg);
},
});
Parameters
参数
jqXHR:
jqXHR:
Its actually an error object which is looks like this
它实际上是一个错误对象,看起来像这样
You can also view this in your own browser console, by using console.log
inside the error
function like:
您还可以在自己的浏览器控制台中查看此内容,方法是使用console.log
以下error
函数:
error: function (jqXHR, exception) {
console.log(jqXHR);
// Your error handling logic here..
}
We are using the status
property from this object to get the error code, like if we get status = 404 this means that requested page could not be found. It doesn't exists at all. Based on that status code we can redirect users to login page or whatever our business logic requires.
我们使用status
这个对象的属性来获取错误代码,比如如果我们得到 status = 404 这意味着无法找到请求的页面。它根本不存在。基于该状态代码,我们可以将用户重定向到登录页面或我们的业务逻辑需要的任何内容。
exception:
例外:
This is string variable which shows the exception type. So, if we are getting 404 error, exception
text would be simply 'error'. Similarly, we might get 'timeout', 'abort' as other exception texts.
这是显示异常类型的字符串变量。因此,如果我们收到 404 错误,exception
文本将只是“错误”。同样,我们可能会得到 'timeout'、'abort' 作为其他异常文本。
Deprecation Notice:The
jqXHR.success()
,jqXHR.error()
, andjqXHR.complete()
callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, usejqXHR.done()
,jqXHR.fail()
, andjqXHR.always()
instead.
取消通知:该
jqXHR.success()
,jqXHR.error()
和jqXHR.complete()
回调弃用的jQuery 1.8。要准备的代码为他们的最终消除,使用jqXHR.done()
,jqXHR.fail()
和jqXHR.always()
来代替。
So, in case you are using jQuery 1.8 or abovewe will need to update the success and error function logic like:-
因此,如果您使用jQuery 1.8 或更高版本,我们将需要更新成功和错误函数逻辑,例如:-
// Assign handlers immediately after making the request,
// and remember the jqXHR object for this request
var jqxhr = $.ajax("some_unknown_page.html")
.done(function (response) {
// success logic here
$('#post').html(response.responseText);
})
.fail(function (jqXHR, exception) {
// Our error logic here
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
$('#post').html(msg);
})
.always(function () {
alert("complete");
});
Hope it helps!
希望能帮助到你!
回答by czerasz
Try this:
尝试这个:
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
If you want to inform your frontend about a validation error, try to return json:
如果您想通知前端有关验证错误的信息,请尝试返回 json:
dataType: 'json',
success: function(data, textStatus, jqXHR) {
console.log(data.error);
}
Your asp script schould return:
你的 asp 脚本应该返回:
{"error": true}
回答by Rich C
Here is how you pull the asp error out.
这是你如何拉出asp错误。
cache: false,
url: "addInterview_Code.asp",
type: "POST",
datatype: "text",
data: strData,
success: function (html) {
alert('successful : ' + html);
$("#result").html("Successful");
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 500) {
alert('Internal error: ' + jqXHR.responseText);
} else {
alert('Unexpected error.');
}
}
回答by increddibelly
you're using a function
你正在使用一个函数
error(error)
but jquery is actually looking for a function with three parameters:
但是 jquery 实际上是在寻找一个带有三个参数的函数:
error(jqXHR, textStatus, errorThrown)
you'll need to add two more parameters.
您需要再添加两个参数。
ALSO: please have a look at all the comments above that mention 'deprecated' :)
另外:请查看上面提到“已弃用”的所有评论:)
$.ajax("www.stackoverflow.com/api/whatever", {
dataType:"JSON"
data: { id=1, name='example' }
}).succes(function (result) {
// use result
}).error(function (jqXHR, textStatus, errorThrown) {
// handle error
});
回答by Praveen Prasad
error(jqXHR, textStatus, errorThrown)
回答by sT0n3
cache: false,
url: "addInterview_Code.asp",
type: "POST",
datatype: "text",
data: strData,
success: function (html) {
alert('successful : ' + html);
$("#result").html("Successful");
},
error: function(data, errorThrown)
{
alert('request failed :'+errorThrown);
}
回答by John Kloian
From jquery.com:
来自 jquery.com:
The jqXHR.success(), jqXHR.error(), and jqXHR.complete()
callback methods introduced injQuery 1.5 are deprecated
as of jQuery 1.8. To prepare your code for their eventual
removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
If you want global handlers you can use:
如果你想要全局处理程序,你可以使用:
.ajaxStart(), .ajaxStop(),
.ajaxComplete(), .ajaxError(),
.ajaxSuccess(), .ajaxSend()