title: 500 Error - JSON object POST to .ASMX webservice
body: <p>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 <strong><em>declare the params as individual variables</em></strong> (eg. <code>int ID, int OrderHeaderID</code>, etc) <strong><em>I do not receive the error</em></strong>.  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.</p>
  
  <p>JS:</p>
  
  <pre><code>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');
          }
      }
  });
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>[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
          }; 
      }
  }
  </code></pre>
  
  <p>Object (abbreviated for simplicity):</p>
  
  <pre><code>public int ID ...
  public int OrderHeaderID ...
  public int StatusID ...
  public decimal DeliveryCharge ...
  public string CreatedBy  ...
  public string ApprovedBy ...
  </code></pre>
  
  <p>Request Data:</p>
  
  <pre><code>{"ID":-1,"OrderHeaderID":5,"StatusID":1,"DeliveryCharge":0,"CreatedBy":"77777777","ApprovedBy":"77777777"}
  </code></pre>
  
  <p>Response Headers:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Response Data:</p>
  
  <pre><code>{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
  </code></pre>
  
  <p><em><strong>FIX:</em></strong></p>
  
  <p>Had to wrap the JSON object so it was recognized on the server:</p>
  
  <pre><code>var params = {
              returnHeader: {
                  ...
              }
          };
  
  ...
  data: JSON.stringify(params),
  ...
  
  {"returnHeader":{"ID":-1,"OrderHeaderID":5,"StatusID":1,"DeliveryCharge":0,"CreatedBy":"77777777","ApprovedBy":"77777777"}}
  </code></pre>