如何在json post请求中使用对象列表或数组到WebService?

时间:2012-06-25 11:13:47

标签: json web-services list post

如标题中所述,我试图通过JavaScript中的JSON / Ajax调用WebService方法,其中包含多个String参数和2个列表或数组。

现在一切正常,单一参数采用以下格式:

var WSParameters = "{pParam1: 'abc', pParam2: 'def'}"

$.ajax({
        type: "POST",
        url: "WebService.asmx/" + webMethod,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        data: WSParameters,
        success: function (result) {
            // stuff to be done on if request was successful
        },
        error: function (message) { alert('Error loading data! ' + message.responseText); }
    });

$.ajax({ type: "POST", url: "WebService.asmx/" + webMethod, contentType: "application/json; charset=utf-8", dataType: "json", async: false, data: WSParameters, success: function (result) { // stuff to be done on if request was successful }, error: function (message) { alert('Error loading data! ' + message.responseText); } });

但是现在我需要为web服务方法提供一个列表/值数组。 Web服务方法如下所示:

[WebMethod(true)]
public void editExistingSystem(System.Collections.Generic.List<String> pFirstList,
        System.Collections.Generic.List<String> pSecondList, 
        String pParam1,
        String pParam2)
    {
      // Stuff
    }

我想它应该是这样的,但显然这不起作用:

[WebMethod(true)] public void editExistingSystem(System.Collections.Generic.List<String> pFirstList, System.Collections.Generic.List<String> pSecondList, String pParam1, String pParam2) { // Stuff }

您能帮我找到正确的语法来定义此参数列表中的列表吗?

非常感谢davance

溴 vm370

1 个答案:

答案 0 :(得分:0)

经常这样,我在提出问题后几乎找到了解决方案...... 对不起打扰你的家伙。我的首发帖子中示例的正确参数字符串为:

var WSParameters = "{pfirstList: ['15','4','13'], pSecondList: ['gr00001_96594737'], pParam1: 'abc', pParam2: 'def'}"

谢谢和最诚挚的问候 vm370