jQuery的AJAX在ASP.NET中使用的customErrors模式="在" [英] jQuery ajax in ASP.NET with customErrors mode="On"

查看:206
本文介绍了jQuery的AJAX在ASP.NET中使用的customErrors模式="在"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法如何找回原来抛出的异常在服务器端做的结果时,
阿贾克斯与jQuery调用和使用


  

的customErrors模式=ON


在web.config中。

如果模式=关我可以使用此功能把错误:

 错误:功能(XHR,状态,错误){
        VAR误差= JSON.parse(xhr.responseText);
        警报(返回Error.message);
}


解决方案

我有同样的问题,发现一对夫妇differens的解决方案:

要么,你可以在web.config中的特定位置禁用的customErrors。

 <位置路径=ajaxReuests>
  <&的System.Web GT;
    <的customErrors模式=关/>
  < /system.web>
< /地点>

不过,要注意的customErrors的<一个href=\"http://weblogs.asp.net/scottgu/archive/2010/09/18/important-asp-net-security-vulnerability.aspx\"相对=nofollow>安全vunerability 禁用自定义错误的时候。

另一个(更安全的解决方案)将一起使用Ajax的完整的回调处理程序上的错误页面meta标签检测错误,并转发到正常误差函数的响应。为了这个工作,你需要包括你的自定义错误页的错误状态code,例如:

 &LT;头=服务器&GT;
  &LT;标题&GT;糟糕&LT; /标题&GT;
  &LT;链接HREF =/ CSS / default.css的rel =stylesheet属性类型=文/ CSS/&GT;
  &LT; META NAME =状态codeCONTENT =500/&GT;
  ...

然后,你可以分析出从XmlHtt prequest.responsText状态code和,如果检测到errorneous状态code调用正常的错误功能。

  $。阿贾克斯({  网址:'/ajax/mydata.ashx',
  数据类型:JSON,  完成:功能(XMLHtt prequest,textStatus){
    VAR状态codeRegex ​​= /content=\"(.*?)\"/;
    如果(状态codeRegex.test(XMLHtt prequest.responseText)及和放大器;
      状态codeRegex.exec(XMLHtt prequest.responseText)[0]!= 200)
      this.error(XMLHtt prequest,textStatus,哦,不......一个错误!);
  },  成功:函数(MYDATA的){
    //做些有益的事...
  },  错误:功能(XMLHtt prequest,textStatus,errorThrown){
    //错误...
  }
});

any idea how to retrieve the original exception thrown on server side when doing
ajax calls with jQuery and using

customErrors mode="On"

in web.config.

If mode="Off" I can take the error using this function:

error: function(xhr, status, error) {
        var error = JSON.parse(xhr.responseText); 
        alert(error.Message);
}

解决方案

I had the same problem and found a couple of differens solutions:

Either, you could disable customerrors for a specific location in web.config.

<location path="ajaxReuests">
  <system.web>
    <customErrors mode="Off" />
  </system.web>
</location>

However, be aware of the customerrors security vunerability when disabling custom errors.

Another (safer solution) would be to use the ajax-complete-callback-handler together with meta tags on the error pages to detect errors and forward the response to the "normal" error function. In order for this to work, you need to include the error status code on your custom error pages, eg:

<head runat="server">
  <title>Oops</title>
  <link href="/css/default.css" rel="stylesheet" type="text/css" />
  <meta name="statuscode" content="500" />
  ...

Then you could parse out the status code from XmlHttpRequest.responsText and invoke the normal error function if an errorneous status code is detected.

$.ajax({

  url: '/ajax/mydata.ashx',
  dataType: 'json',

  complete: function(XMLHttpRequest, textStatus) {
    var statuscodeRegex = /content="(.*?)"/;
    if (statuscodeRegex.test(XMLHttpRequest.responseText) &&
      statuscodeRegex.exec(XMLHttpRequest.responseText)[0] != 200)
      this.error(XMLHttpRequest, textStatus, "Oh no... an error!");
  },

  success: function(myData) {
    //Do something useful...
  },

  error: function(XMLHttpRequest, textStatus, errorThrown) {
    //Error...
  }
});

这篇关于jQuery的AJAX在ASP.NET中使用的customErrors模式=&QUOT;在&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆