0

我尝试将包含两个整数的数组传递给 web 服务的函数:

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public bool sample(int number, int[] numbers)
{
    return (numbers.Sum() > number);
}

我用 jquery.ajax 来做:

var arr = ['20', '89'];

$.ajax({ url: "Registration.asmx/sample",
    data: jQuery.param({ 'number': '300', 'numbers[]': arr }),
    type: "POST",
    success: function (data) {
        $('#result').html(data.toString());
    },
    error: function (xhr, ajaxOptions, thrownError) {
        $('#result').html(jQuery.param({ 'number': '300', 'numbers': arr }) + "<br/><br/>" +
                      thrownError + "<br/><br/>" +
                      xhr.responseText);
    }
});

但我总是收到关于空数组的服务器内部错误(我认为)没有任何

4

2 回答 2

0

try changing :

data: jQuery.param({ 'number': '300', 'numbers[]': arr }),

to:

data: jQuery.param({ 'number': '300', 'numbers': arr }),

EDIT:

You are stating that the method accepts GET requests:

[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]

But in the javascript you are using POST:

type: "POST",

change it to:

type: "GET",
于 2012-07-10T11:10:52.727 回答
0

现在我看到我必须以某种方式创建以下网址:Registration.asmx/sample?number=30&numbers=55&numbers=88 (numbers:30, numbers[55,88])

我只知道一种方法:创建一个字符串:/你知道可以处理这个问题的 js/jquery 方法吗?(jquery.param 创建一个类似 Registration.asmx/sample?number=30&numbers[]=55&numbers[]=88 的 url,这是不可接受的..)

于 2012-07-10T15:35:52.560 回答