我有以下签名的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;
}
}