500 Error - JSON object POST to .ASMX webservice

13,719

You're only passing in the object's properties, not the entire object container. So, the web method is expecting something like this instead:

{returnHeader:{"ID":-1,"OrderHeaderID":5,"StatusID":1,"DeliveryCharge":0,"CreatedBy":"77777777","ApprovedBy":"77777777"}}
Share:
13,719
Terry
Author by

Terry

I build web applications for the modern enterprise. I value efficiency, stability, performance & elegance. GitHub Twitter

Updated on June 05, 2022

Comments

  • Terry
    Terry almost 2 years

    Keep the question here short and sweet. I'm Getting a 500 error when I try and pass a JSON object to an ASMX webservice. Note that if I declare the params as individual variables (eg. int ID, int OrderHeaderID, etc) I do not receive the error. I can't see why the problem is happening, I have successfully passed objects in this manner before, possibly with different syntax but I can't recall.

    JS:

    var returnHeader = {
        ID: -1,
        OrderHeaderID: parseInt(getQueryStringKey('OrderID')),
        StatusID: 1,
        DeliveryCharge: 0,
        CreatedBy: $('span[id$="lblHidUsername"]').text(),
        ApprovedBy: $('span[id$="lblHidUsername"]').text()
    };
    
    $.ajax({
        type: "POST",
        url: 'Order.asmx/SaveReturnHeader',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify(returnHeader),
        success: function (result) {
            if (result.Status == 'OK') {
                GetReturns();
            }
            else {
                $('#divMessage').show().html(result.Data.Message).addClass('error');
            }
        },
        error: function (x, e) {
            if (x.status == 500) {
                $('#divMessage').show().html('An unexpected server error has occurred, please contact support').addClass('error');
            }
        }
    });
    

    Server:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public object SaveReturnHeader(BEReturnHeader returnHeader)
    {
        try
        {
            return new
            {
                Status = "OK",
                Data = ""
            };                
        }
        catch (Exception ex)
        {
            return new
            {
                Status = "ERROR",
                Data = ex
            }; 
        }
    }
    

    Object (abbreviated for simplicity):

    public int ID ...
    public int OrderHeaderID ...
    public int StatusID ...
    public decimal DeliveryCharge ...
    public string CreatedBy  ...
    public string ApprovedBy ...
    

    Request Data:

    {"ID":-1,"OrderHeaderID":5,"StatusID":1,"DeliveryCharge":0,"CreatedBy":"77777777","ApprovedBy":"77777777"}
    

    Response Headers:

    HTTP/1.1 500 Internal Server Error
    Date: Mon, 05 Dec 2011 16:38:36 GMT
    Server: Microsoft-IIS/6.0
    X-Powered-By: ASP.NET
    X-AspNet-Version: 2.0.50727
    jsonerror: true
    Cache-Control: private
    Content-Type: application/json
    Content-Length: 91
    

    Response Data:

    {"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
    

    FIX:

    Had to wrap the JSON object so it was recognized on the server:

    var params = {
                returnHeader: {
                    ...
                }
            };
    
    ...
    data: JSON.stringify(params),
    ...
    
    {"returnHeader":{"ID":-1,"OrderHeaderID":5,"StatusID":1,"DeliveryCharge":0,"CreatedBy":"77777777","ApprovedBy":"77777777"}}
    
  • Terry
    Terry over 12 years
    That was the problem. Lame, I assumed it was smart enough to realize only a single object was being passed. Thanks Scott. The joys of working in .NET 2.0...
  • Terry
    Terry over 12 years
    ScottE: having the same problem now with an array but I named it correctly, any ideas? DATA: {"ReturnDetails":[{"ReturnHeaderID":5,"SKU":537258,"ReasonCo‌​de":1,"Quantity":1,"‌​Comment1":"","Commen‌​t2":"","Comment3":""‌​},{"ReturnHeaderID":‌​5,"SKU":467662,"Reas‌​onCode":1,"Quantity"‌​:1,"Comment1":"","Co‌​mment2":"","Comment3‌​":""}]} Function: public object SaveReturnDetails(List<BEReturnDetail> ReturnDetails)
  • Terry
    Terry over 12 years
    PS. If you know the problem I can make a new question so you get credit for a full answer.
  • ScottE
    ScottE over 12 years
    ReturnHeaderID should be just ID, according to your object definition. Also, it looks like you have some extra fields in there?
  • Terry
    Terry over 12 years
    Nah these are ReturnDetail objects, not ReturnHeader. Sorry I know it's a bit confusing. That being said, in the meantime I figured out there was a legit server side code error that was not being thrown even though I'm catching exceptions and returning them as successfully objects. Turns out there was nothing wrong with my syntax. Thanks for looking again, sorry for wasting your time.