gpt4 book ai didi

jquery - 500 错误 - JSON 对象 POST 到 .ASMX Web 服务

转载 作者:行者123 更新时间:2023-12-01 00:08:08 25 4
gpt4 key购买 nike

这里的问题要简短而亲切。当我尝试将 JSON 对象传递给 ASMX Web 服务时,出现 500 错误。请注意,如果我将参数声明为单独变量(例如int ID、int OrderHeaderID等)我会这样做没有收到错误。我不明白为什么会发生这个问题,我之前已经成功地以这种方式传递了对象,可能使用了不同的语法,但我不记得了。

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

服务器:

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

对象(为简单起见缩写):

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

请求数据:

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

响应 header :

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

响应数据:

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

修复:

必须包装 JSON 对象,以便服务器能够识别它:

var params = {
returnHeader: {
...
}
};

...
data: JSON.stringify(params),
...

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

最佳答案

您只传递对象的属性,而不是整个对象容器。因此,Web 方法需要类似这样的内容:

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

关于jquery - 500 错误 - JSON 对象 POST 到 .ASMX Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8388943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com