1

I have a web app running asp.net Framework 4.5, jQuery 1.8.2, which makes a jQuery Ajax call to add a record to a SQL 2012 database. That process may produce a specific error that I want passed back to the user. Seems simple enough. So after recording the error in a custom log which it does nicely, I throw the error back to the jQuery Ajax caller and use the jQuery error: function (xhr, errStatus, thrownError) to try and parse it with JSON and show it to the user. It is working beautifully on my development machine; however, when I deploy the application to our test server (running OS 2012 and IIS 8), that error is ignored and all I get is 500: Internal Server Error instead of my custom error. I put in some checks for the status, and xhr.responseText, and the responseText is exactly what is being shown from the test server (Internal server error). I am at a loss as to what exists on my machine that is different on the test server resulting in this situation.

I tried to remove the try, catch entirely from the WebMethod.addPerson function so it would return the exact error back to jQuery and that did not change a thing; still got the Internal server error instead of the specific error message. It does not matter if I am using IE, Google, or Firefox.

I am at a loss as to what is preventing the actual error from the database to be returned to jQuery Ajax caller. Is it a server setting or something I need to add to the web config? Do I need to add some options to the jQuery Ajax setup? Other cause?

Thank you in advance for your assistance.

PLEASE NOTE: the code I present here is a very simplified version of the actual code; however, it is not significantly different. What I changed from the actual code is just parameter value types and names.

function addPerson(personName, personPhone) {
    try{
            var params = "'name':'" + personName + "','phone':'" + personPhone + "'";
            $.ajax({
                type: "POST",
                url: "WebMethods.aspx/addPerson",
                data: "{" + params + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    personId = msg.d;
                    if (personId > 0) {
                        //Do stuff and things on web page                   }
                    else { alert("Sorry, but the process did not return a key value and the person could not be added."); }
                },
                error: function (xhr, errStatus, thrownError) {
                    var r = jQuery.parseJSON(xhr.responseText);
                    alert(r.Message);
                }
            });
    }
    catch (err) { alert(err.description); }
}

.... WebMethod Code ....

[System.Web.Services.WebMethod(EnableSession = true)]
[System.Web.Script.Services.ScriptMethod]
public static Int32 addPerson(string name, string phone)
{
    try
    {
            BusinessLogic.BusinessContactLogic bl = new BusinessLogic.BusinessContactLogic();
            return bl.addPerson(name, phone);
        }
        catch (Exception exc)
        {
            Lib.Errors.LogError(exc, "WebMethods.addPerson");
            //Tried this next line instead of rethrowing the error and there was no difference
            //throw new HttpException(410, "Person already exists with that name and phone number.");
             throw exc;
        }
    }
}
Edward
  • 261
  • 2
  • 10
  • Whatever is happening here it is on the server side, not with you're AJAX. My guess is that it's IIS overriding/ignoring your error handling in the production environment. Answers might be 1. [here](http://stackoverflow.com/questions/2631675/how-do-i-stop-iis7-from-putting-out-its-own-404-before-my-mvc-app-gets-a-chance) and 2. [here](http://stackoverflow.com/questions/25356472/net-mvc-throws-500-server-error-instead-of-404-not-found-when-cannot-match-a-co) – wahwahwah Mar 08 '16 at 19:29
  • Tried to add that code change but got the same result; no custom error message. – Edward Mar 10 '16 at 01:00
  • After adding the code to the Web Method and getting the same result; I decided to return a specific set of integer values from the WebMethod and then take action on the client based on the specific integer value returned and that worked very well. Thanks for the ideas though. – Edward Mar 10 '16 at 01:21

0 Answers0