Jquery ajax in ASP.NET with customErrors mode = "On"

any idea how to get the original exception thrown on the server side while executing ajax calls jQuery and uses

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);
}
+3
source share
2 answers

It is best to move the server method to another host with its own web.config. If you've never done this before, ASP.NET Web Services is a good place to start.

, try ... catch , . : $.ajax success, , , ( HTTP-, DNS ..).

+1

, :

web.config.

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

vunerability.

( ) ajax-complete-callback - "" . , , :

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

XmlHttpRequest.responsText , .

$.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...
  }
});
+1

Source: https://habr.com/ru/post/1742108/


All Articles