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