skip to Main Content

I called a ASP.net 4.0 web service using ajax(). I can get my error message locally, but when I deploy to server, the error was replaced by 500 Internal Server Error with message "There was an error processing the request.". My code is shown below. Please help.

$.ajax({
    type: 'POST',
    url: '../WebServices/UpdateData.asmx/UpdateSomething',
    data: "{ 'id': 100, 'something': 'Some text'} "
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        //--Do something
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.responseJSON.Message || xhr.statusText);
    }});


[WebMethod]
public string UpdateSomething(int id, string something){
  ....
  throw new ApplicationException("My error message");
}

2

Answers


  1. Chosen as BEST ANSWER

    I finally found that the error message will became Internal Server Error when I set in web.config <CustomErrors Mode="On/RemoteOnly" />

    Then I got an alternative solution on this post. Asp.net web method ajax call show stack trace and actual exception while custom errors mode on


  2. Are you sure that the path name of going down one folder with ".." is correct?

    What occurs if you up 2 folders, or say down at root level already?

    One nice way to "resolve" the different possible path names is to replace the hard coded path name with a server side expression that will ALWAYS resolve to the correct path name starting from root folder. Often when you deploy, you might say be deploying to a sub folder on the production web server, and thus your path names can become different. The other issue is what happens when you using a master page, or using your sample code from a different web page location. Unless you are 100% sure your existing code is always running one folder up from the asmx page, then often such calls will fail after publishing, since your folder setup changes somewhat.

    The instant you change the location, or publish to say a extra folder down on the server, then your hard coded path name will break.

    Hence, I suggest adopting a server side URL expression.

    Hence, try this:

    url: '<%= ResolveUrl("~/WebServices/UpdateData.asmx/UpdateSomething") %>',
    

    So, the above will work, and work regardless of the location or nesting as to where you publish the production web site to the server. And it will also work if you happen to have moved the page, or that you using a master page, and once again, the nesting of the child page can well be different then the master page nesting.

    The server side "~/" starts from root of the published application location, and use of "~" is not supported in JavaScript, but it most certainly is for server side expressions.

    Hence, give the above a try, it should fix such path issues.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search