开发者

Calling asmx with class array from jQuery ajax

I have a WebMethod with the following signature:

[WebMethod]
    public void SaveRoute(int id, Coordinates[] coordinates)
    {
    }

Where Coordinates is:

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

I want to call this from a jQuery ajax method like this one:

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

What would I have to supply in the data property for a correct deserialisation?

I have now solved it:

Thanks for all the help, I eventually managed to get what I wanted with the following code:

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

I reverse engineered the JSON by sending some down with another method:

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

It is also important that you have a public parameterless constructor in your class and your property's setters are public:

/// <summary>
/// A position on the globe
/// </summary>
public class Coordinates
{
    public decimal Longitude { get; set; }
    pub开发者_开发技巧lic decimal Latitude { get; set; }

    public Coordinates()
    {
    }

    public Coordinates(decimal latitude, decimal longitude)
    {
        this.Longitude = longitude;
        this.Latitude = latitude;
    }
}


I recommend using JSON.stringify to build your "JSON" strings. With JSON.stringify you can convert a array of coordinates to a JSON string representation. Example:

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

If you place your SaveRoute web method on a aspx website you can use ScriptManager to generate page methods and script types (see MSDN documentation for GenerateScriptTypeAttribute).


Provide it as an object, not a string:

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


Since coordinates is an array try this

data: '{ "id":1, ,"coordinates": [ {"Longitude":123,"Latitude":456} ] }'

For a more complex objects you can use JSON binder from the below link

http://community.codesmithtools.com/CodeSmith_Community/b/tdupont/archive/2009/08/10/mvc-custom-json-binder.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜