简体   繁体   English

解析jQuery.ajax错误响应消息

[英]Parsing a jQuery.ajax error response message

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. 我正在模拟这种情况,在ajax调用中出现错误。 I get a response message in case of an error and I need to assign this html to an element on the page. 如果发生错误,我会收到响应消息,我需要将此html分配给页面上的元素。

Here is what I get in a message: 这是我收到的消息: 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的值以显示在页面上?

In JavaScript variable names are case sensitive. 在JavaScript中,变量名称区分大小写。 In your example, you were trying to access the responseText field on the msg object, but you had a capital 'R'. 在您的示例中,您试图访问msg对象上的responseText字段,但是您使用了大写的“ R”。 Try this instead: 尝试以下方法:

msg['responseText']

Or in much better style: 或采用更好的样式:

msg.responseText

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);

}

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

msg['responseText']
<div id='error'></div>

假设您在msg出现错误

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM