Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# exception not captured correctly by jquery ajax

ok i have a problem with the production environment and how jquery ajax captures the error.

In my development environment on my local machine when the jquery ajax calls a webservice [webmethod] and the webmethod throws a specific error, the jquery ajax captures that correctly but in production envirnment the jquery ajax captures the error but the message is generic "There was an error processing the request." with "Internal Server Error"

for example

The c# code

[WebMethod]
public String dostuff(String Auth){
// some code here
// if error occurs
throw new Exception("Some specific error message");
return "ok";
}

the jquery ajax call

    var data = JSON.stringify({ Auth: "some data here" }, null);

    $.ajax({
        type: "POST",
        url: '/Services/memberService.asmx/dostuff',
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            alert(result.d); // alert ok
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("responseText=" + XMLHttpRequest.responseText + "\n textStatus=" + textStatus + "\n errorThrown=" + errorThrown);
        }
    });

on my local machine the XMLHttpRequest.responseText is "Some specific error message"

on the production environment the XMLHttpRequest.responseText is "There was an error processing the request."

the local environment is windows 7 home premium with IIS 7.5.7600.16385

the production environment is Windows Server 2008 R2 with IIS 7.5.7600.16385 (same as development environment)

why the difference and how to make the production environment throw the specific error?

********* just a follow up .. tnx Justin *************

i added web.config file inside the services folder (Doh... wonder why i didn't think about this)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
      <customErrors mode="Off" />
    </system.web>
</configuration>
like image 526
robert Avatar asked Feb 22 '12 01:02

robert


1 Answers

By default, .NET Applications are configured to only show specific Exception information to the local server.

This is so that attackers can't get specific Exception information about your site based on their attacks. It is typically considered a bad idea to change that behavior.

If you must, though, you can change:

<customErrors mode="RemoteOnly" />

To:

<customErrors mode="Off" />

And if you want to show the errors for the Services folder:

<location Path="/Services">
    <system.web>
        <customErrors mode="Off" />
    </system.web>
</location>
like image 69
Justin Niessner Avatar answered Oct 14 '22 03:10

Justin Niessner