从jQuery ajax调用带有类数组的asmx

从jQuery ajax调用带有类数组的asmx,jquery,ajax,web-services,Jquery,Ajax,Web Services,我有一个具有以下签名的WebMethod: [WebMethod] public void SaveRoute(int id, Coordinates[] coordinates) { } 其中坐标为: /// <summary> /// A position on the globe /// </summary> public class Coordinates { public decimal Longitude { get; pro

我有一个具有以下签名的WebMethod:

[WebMethod]
    public void SaveRoute(int id, Coordinates[] coordinates)
    {
    }
其中坐标为:

/// <summary>
/// A position on the globe
/// </summary>
public class Coordinates
{
    public decimal Longitude { get; protected set; }
    public decimal Latitude { get; protected set; }

    public Coordinates(decimal latitude, decimal longitude)
    {
        this.Longitude = longitude;
        this.Latitude = latitude;
    }
}
对于正确的反序列化,我必须在数据属性中提供什么

我现在已经解决了这个问题:

感谢所有的帮助,我最终通过以下代码获得了我想要的:

var coords = new Array();
            for (ckey in flightPlanCoordinates) {
                var thisWaypoint = { "Longitude": flightPlanCoordinates[ckey].lng(), "Latitude": flightPlanCoordinates[ckey].lat() };
                coords.push(thisWaypoint);
            }
            var data = { "id": 1, "coordinates": coords };
            if (flightPlanCoordinates) {

                $.ajax({
                    type: "POST",
                    url: "Routes.asmx/SaveRoute",
                    cache: false,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(data),
                    dataType: "json",
                    success: handleHtml,
                    error: ajaxFailed
                });
            }
我通过使用另一种方法发送一些JSON来反向工程JSON:

[WebMethod]
    public Coordinates[] Get()
    {
        return new Coordinates[]
        {
            new Coordinates(123, 456),
            new Coordinates(789, 741)
        };
    }
同样重要的是,在类中有一个公共的无参数构造函数,并且属性的setter是公共的:

/// <summary>
/// A position on the globe
/// </summary>
public class Coordinates
{
    public decimal Longitude { get; set; }
    public decimal Latitude { get; set; }

    public Coordinates()
    {
    }

    public Coordinates(decimal latitude, decimal longitude)
    {
        this.Longitude = longitude;
        this.Latitude = latitude;
    }
}
//
///地球上的位置
/// 
公共类坐标
{
公共十进制经度{get;set;}
公共十进制纬度{get;set;}
公共坐标()
{
}
公共坐标(十进制纬度、十进制经度)
{
这个。经度=经度;
这个。纬度=纬度;
}
}

将其作为对象而不是字符串提供:

$.ajax({
    type: "POST",
    url: "Routes.asmx/SaveRoute",
    cache: false,
    contentType: "application/json; charset=utf-8",
    data: {"id":1, "coordinates": ["Longitude":123, "Latitude":456]},
    dataType: "json",
    success: handleHtml,
    error: ajaxFailed
});

由于
坐标
是一个数组,请尝试以下操作

data: '{ "id":1, ,"coordinates": [ {"Longitude":123,"Latitude":456} ] }'
对于更复杂的对象,您可以从下面的链接使用
JSON
binder


我建议使用JSON.stringify来构建“JSON”字符串。 使用JSON.stringify,您可以将坐标数组转换为JSON字符串表示形式。 例如:

如果将SaveRoute web方法放在aspx网站上,则可以使用ScriptManager生成 页面方法和脚本类型(参见GenerateScriptTypeAttribute文档)

data: '{ "id":1, ,"coordinates": [ {"Longitude":123,"Latitude":456} ] }'
var id = 2;
var coords = new Array();

coords[0] = { Longitude: 40.0, Latitude: 76.0 };
coords[1] = { Longitude: 42.0, Latitude: 77.0 };

$.ajax({
    type: "POST",
    url: "Routes.asmx/SaveRoute",
    cache: false,
    contentType: "application/json; charset=utf-8",
    data: '{ "id":' + JSON.stringify(id) + ', "coordinates":' + JSON.stringify(coords) + '}',
    dataType: "json",
    success: handleHtml,
    error: ajaxFailed });