jQuery $.ajax() 函数的错误信息来源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2113382/
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() function's error message source
提问by sarsnake
In the case of failure, the code goes something like:
在失败的情况下,代码类似于:
error: function(msg)
where does the msg
come from?
在什么地方msg
来的呢?
EDIT:
编辑:
I am using this function ($ajax) to call a WEB SERVICE. So if whoever voted this down could explain to me where msg comes from that would be great! Do I set it in web service? If so, how? Please don't copy and paste the definitions.
我正在使用此函数 ($ajax) 来调用 WEB 服务。因此,如果投票否决的人可以向我解释 msg 的来源,那就太好了!我是否在网络服务中设置它?如果是这样,如何?请不要复制和粘贴定义。
回答by Patrick Karcher
From the jquery documentation:
从 jquery 文档:
error(XMLHttpRequest, textStatus, errorThrown) Function
A function to be called if the request fails. The function is passed three arguments:
- The XMLHttpRequest object,
- a string describing the type of error that occurred, and
- an optional exception object, if one occurred.
Possible values for the second argument (besides null) are "timeout", "error", "notmodified" and "parsererror". This is an Ajax Event.
error(XMLHttpRequest, textStatus, errorThrown) Function
请求失败时调用的函数。该函数传递了三个参数:
- XMLHttpRequest 对象,
- 一个描述发生的错误类型的字符串,以及
- 一个可选的异常对象(如果发生了)。
第二个参数(除了 null)的可能值是“超时”、“错误”、“未修改”和“解析器错误”。这是一个 Ajax 事件。
When you indicate just one parameter, it will be the xmlHttpRequestObject
. You can get some good information from that. Sadly, most documentation out there doesn't have you set this up correctly. A good default template is:
当您仅指定一个参数时,它将是xmlHttpRequestObject
. 你可以从中得到一些很好的信息。遗憾的是,大多数文档都没有让您正确设置。一个好的默认模板是:
error:function (xhRequest, errorText, thrownError)
Some good info you can get from xhRequest are:
您可以从 xhRequest 获得的一些好信息是:
.status
: 404: "not found", 500: "server error". This can sometimes be a big help..responseText
is information from the server, often useless in the case of an error but sometimes can be helpful.
.status
:404:“未找到”,500:“服务器错误”。这有时会很有帮助。.responseText
是来自服务器的信息,在出现错误时通常无用,但有时会有所帮助。
The second value, a string, is sometimes helpful. Ah, I guess the possible values are mentioned in the documentation.
第二个值,一个字符串,有时很有用。啊,我猜文档中提到了可能的值。
The 3rd parameter, whenever I checked it out, has always been undefined. I don't think it's ever useful.
每当我检查它时,第三个参数始终未定义。我不认为它永远有用。
回答by Vladimir Buskin
I think the 3rd parameter errorThrownin the error callback function
我认为错误回调函数中的第三个参数errorThrown
error(XMLHttpRequest, textStatus, errorThrown)
is exactly to send the text error message from server.
正是从服务器发送文本错误消息。
So if on a server you set:
因此,如果在您设置的服务器上:
Response.Status = 403;
Response.StatusDescription = "Password is not correct";
on a client you get:
在客户上你得到:
textStatus => "error",
errorThrown => "Password is not correct"
Server part for Asp.net MVC will be:
Asp.net MVC 的服务器部分将是:
return new HttpStatusCodeResult(403, "Password is not correct");
回答by sdmiller
THe Message is a return from the actual server side function that you are querying in your ajax call.
消息是您在 ajax 调用中查询的实际服务器端函数的返回。
That way you can get the error or any other info on whether or not the server side code did what it was supposed to do.
这样你就可以得到错误或任何其他关于服务器端代码是否做了它应该做的事情的信息。
Say if you return a string "success"
假设你返回一个字符串“成功”
msg.val()
will equal "success"
msg.val()
将等于“成功”
hope that helps
希望有帮助
回答by Eddie
the ajax return variable (in this case 'msg') is the output returned from the AJAX call - in the case of an ajax error, this would probably be a server error.
ajax 返回变量(在本例中为“msg”)是从 AJAX 调用返回的输出 - 在 ajax 错误的情况下,这可能是服务器错误。
回答by Per H
Actually, the correct way is:
其实正确的做法是:
error: function(req, status, error) {
}
From the jQuery API:
来自 jQuery API:
A function to be called if the request fails. The function is passed three arguments: The XMLHttpRequest object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "notmodified" and "parsererror". This is an Ajax Event.
请求失败时调用的函数。该函数传递了三个参数:XMLHttpRequest 对象、一个描述发生的错误类型的字符串和一个可选的异常对象(如果发生)。第二个参数(除了 null)的可能值是“超时”、“错误”、“未修改”和“解析器错误”。这是一个 Ajax 事件。