2011-08-18 64 views
1

我有以下签名的WebMethod:与jQuery的AJAX类数组调用ASMX

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

我想从这样一个jQuery AJAX方法调用此一个:

$.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 
       }); 

在正确的反序列化中,我需要在数据属性中提供什么?

我现在已经解决了它:

感谢所有帮助,我最终设法得到我想要用下面的代码:

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另一种方法:

[WebMethod] 
    public Coordinates[] Get() 
    { 
     return new Coordinates[] 
     { 
      new Coordinates(123, 456), 
      new Coordinates(789, 741) 
     }; 
    } 

同样重要的是,你在你的班上有一个公共的参数的构造函数和你的财产的制定者是公开的:

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

回答

2

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

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

如果你把你的SaveRoute Web方法上的ASPX网站,你可以使用的ScriptManager产生 页面的方法和脚本类型(参见GenerateScriptTypeAttribute MSDN文档)。

1

提供它作为一个对象,而不是字符串:

$.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 
});