javascript 解析 jQuery.ajax 错误响应消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7917179/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:42:24  来源:igfitidea点击:

Parsing a jQuery.ajax error response message

javascriptjquery

提问by Maxim V. Pavlov

I am posting data from one ASP.NET page to another via jQuery ajax call in a form of JSON.

我正在通过 JSON 形式的 jQuery ajax 调用将数据从一个 ASP.NET 页面发布到另一个页面。

I am simulating the situation, where is get an error on ajax call. I get a response message in case of an error and I need to assign this html to an element on the page.

我正在模拟这种情况,在 ajax 调用中出现错误。如果出现错误,我会收到一条响应消息,我需要将此 html 分配给页面上的元素。

Here is what I get in a message: jquery.ajax response

这是我在一条消息中得到的信息: jquery.ajax 响应

I have the msg javascript variable, that, when looked up via Chrome debugger shows me that it contains info I need in responseText.

我有 msg javascript 变量,当通过 Chrome 调试器查找时,它向我显示它包含我需要的信息responseText

How do I get the value of responseText to display on the page?

如何获取 responseText 的值以显示在页面上?

回答by a'r

In JavaScript variable names are case sensitive. In your example, you were trying to access the responseTextfield on the msg object, but you had a capital 'R'. Try this instead:

在 JavaScript 中,变量名区分大小写。在您的示例中,您试图访问responseTextmsg 对象上的字段,但您有一个大写的“R”。试试这个:

msg['responseText']

Or in much better style:

或者更好的风格:

msg.responseText

回答by Michael D. Irizarry

Since its an Object use the dot notation to access it like xhr.responseText

由于它是一个对象,因此使用点符号来访问它,例如 xhr.responseText

error: function(xhr, status, error) {

  var err = eval("(" + xhr.responseText + ")");

  alert(err.Message);

}

回答by ZenMaster

You can see in the code just under your mouse pointer - only with "r", not capital "R":

您可以在鼠标指针下方的代码中看到 - 只有“r”,而不是大写的“R”:

msg['responseText']

回答by Sedat Ba?ar

<div id='error'></div>

assume that u got error in msg

假设你有错误 msg

$('#error').html(msg.responseText)