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