500错误 ​​- JSON对象POST到.A​​SMX web服务 [英] 500 Error - JSON object POST to .ASMX webservice

查看:295
本文介绍了500错误 ​​- JSON对象POST到.A​​SMX web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请这里的问题简短而亲切。我得到一个500错误,当我尝试和JSON对象传递到ASMX web服务。需要注意的是,如果我的声明PARAMS作为单个变量(如内部ID,INT OrderHeaderID 等)<强>的我没有收到错误的。我不明白为什么这个问题正在发生的事情,我已经成功地以这种方式传递的对象之前,可能有不同的语法,但我不记得。

记者:

  VAR returnHeader = {
    ID:-1,
    OrderHeaderID形式:parseInt(getQueryStringKey(订单ID)),
    StatusID:1,
    DeliveryCharge:0,
    CreatedBy:$('跨度[ID $ =lblHidUsername])文本()
    都批准了:$('跨度[ID $ =lblHidUsername])文本()
};

$阿贾克斯({
    键入:POST,
    网址:Order.asmx / SaveReturnHeader',
    的contentType:应用/ JSON的;字符集= UTF-8,
    数据类型:JSON,
    数据:JSON.stringify(returnHeader)
    成功:函数(结果){
        如果(result.Status =='OK'){
            GetReturns();
        }
        其他 {
            。$('#divMessage)显示()HTML(result.Data.Message).addClass('错误');
        }
    },
    错误:函数(X,E){
        如果(x.status == 500){
            $('#divMessage)显示()HTML(发生意外的服务器错误,请联系支持')addClass(错误)。;
        }
    }
});
 

服务器:

  [WebMethod的]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)
公共对象SaveReturnHeader(BEReturnHeader returnHeader)
{
    尝试
    {
        返回新
        {
            状态为正常,
            数据=
        };
    }
    赶上(例外前)
    {
        返回新
        {
            状态为错误,
            数据=前
        };
    }
}
 

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

 公众诠释ID ...
公众诠释OrderHeaderID ...
公众诠释StatusID ...
公共小数DeliveryCharge ...
公共字符串CreatedBy ...
公共字符串...都批准
 

请求数据:

<$p$p><$c$c>{"ID":-1,"OrderHeaderID":5,"StatusID":1,"DeliveryCharge":0,"CreatedBy":"77777777","ApprovedBy":"77777777"}

响应头:

  HTTP / 1.1 500内部服务器错误
日期:星期一,2011年12月5日16点38分36秒GMT
服务器:Microsoft-IIS / 6.0
的X已启动方式:ASP.NET
的X ASPNET-版本:2.0.50727
jsonerror:真
缓存控制:私人
内容类型:应用程序/ JSON
内容长度:91
 

响应数据:

  {消息:有一个错误处理请求,堆栈跟踪:,ExceptionType:}
 

FIX:

只好包裹JSON对象,因此它被确认的服务器上:

  VAR PARAMS = {
            returnHeader:{
                ...
            }
        };

...
数据:JSON.stringify(PARAMS)
...

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

解决方案

你只是传递对象的属性,而不是整个对象容器。因此,Web方法是期待这样的事情,而不是:

<$p$p><$c$c>{returnHeader:{"ID":-1,"OrderHeaderID":5,"StatusID":1,"DeliveryCharge":0,"CreatedBy":"77777777","ApprovedBy":"77777777"}}

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

解决方案

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

这篇关于500错误 ​​- JSON对象POST到.A​​SMX web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆