将Array传递给Asp webservice

时间:2012-04-17 08:34:50

标签: jquery asp.net

我有数组需要将该数组传递给我的ASP.Net Webservice

我的数组看起来像这样:

{
    "lists" [
        {"F0":"LpSeries", "F1":"Description"},
        {"F0":"Second","F1":"StringValue"}
    ]
}

我使用这样的javascript:

$.ajax({
    type: "POST",
    contentType: "application/json",
    data: jsonText,
    url: "LPService.asmx/GetDataTableFromArray",
    dataType: "json",
    success: function (data) {
        alert(data.d);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        debugger;
    }
});

在网络服务中,我写得像这样

[WebInvoke(Method = "POST", UriTemplate = "")]
public DataTable X(RootObject root)
{
    DataTable dt = new DataTable();
    return dt;
    // do something with your root objects or its child objects...

    //return new HttpResponseMessage(HttpStatusCode.Created);
}

这里我为root对象创建了一个类,看起来像这个

public class RootObject
{
    public List<Result> Results { get; set; }
}

public class Result
{
    public string F0 { get; set; }
    public string F1 { get; set; }
    public string F2 { get; set; }
    public string F3 { get; set; }
    public string F4 { get; set; }
    public string F5 { get; set; }
    public string F6 { get; set; }
    public string F7 { get; set; }
}

我无法获得价值。请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

尝试这样做:

var myArray1 = new Array();    
var myArray2 = new Array();    
data: "{'myArray1':"+JSON.stringify(myArray1)+",'myArray2':"+JSON.stringify(myArray2)+"}",

在您的网络方法中:

[System.Web.Services.WebMethod]
public static string SavePage(List<string> myArray1, List<string> myArray2)
{
return myArray1;
}