使用jQuery AJAX显示正确的错误消息 [英] Show proper error messages with jQuery AJAX
问题描述
当前,如果我对ajax请求有错误,例如提交表单(我正在使用ASP.NET MVC 3),它将显示类似内部服务器错误"的信息.
Currently if I have an error with an ajax request such as submitting a form (I'm using ASP.NET MVC 3) it will show something like 'Internal Server Error'.
但是,如果我不使用Ajax进行提交,则.net将显示实际的服务器错误.
But if I do the submit without using Ajax then .net will show the actual server error.
关于如何显示错误的任何想法?因此,我的用户可以报告问题,而不仅仅是说内部服务器错误"
Any ideas on how I could perhaps get the error to show? So my users can report the problem instead of only being able to say 'internal server error'
谢谢
推荐答案
尽管我不建议向用户显示一个错误号以及实际的响应错误消息,但这是通过添加error
回调
Although I wouldnt recommend showing the user an error number with the actual response error message this is how you can do this by adding the error
callback
error: function(xhr,err){
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
alert("responseText: "+xhr.responseText);
}
xhr 是XmlHttpRequest.
xhr is XmlHttpRequest.
readyState 的值为1:正在加载,2:已加载,3:互动,4:已完成.
readyState values are 1:loading, 2:loaded, 3:interactive, 4:complete.
状态是HTTP状态号,即404:未找到; 500:服务器错误; 200:确定.
status is the HTTP status number, i.e. 404: not found, 500: server error, 200: ok.
responseText 是服务器的响应-可以是Web服务中的文本或JSON,也可以是Web服务器中的HTML.
responseText is the response from the server - this could be text or JSON from the web service, or HTML from the web server.
这篇关于使用jQuery AJAX显示正确的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!