使用jQuery AJAX将字符串数组传递给具有可变参数数量的webmethod

时间:2021-05-17 23:16:04

I'm trying to pass an array of string parameters to a C# ASP.NET web service using jQuery Ajax. Here is my sample web method. Note that the function accepts a variable number of parameters. I get a 500 Internal Server Error in Chrome's javascript console when I run the jquery. I'm using jquery 1.6.2 and .NET3.5

我正在尝试将一系列字符串参数传递给c# ASP。使用jQuery Ajax的NET web服务。这是我的示例web方法。注意,该函数接受可变数量的参数。当我运行jquery时,在Chrome的javascript控制台会出现500个内部服务器错误。我使用的是jquery 1.6.2和。net3.5

[WebMethod]
public string Concat(params string[] arr)
{
    string result = "";
    for (int i = 0; i < arr.Length; i++)
    {
        result += arr[i];
    }
    return result;
}

Here is the jquery:

jquery:

$(document).ready(function() {

    var myCars=new Array(); 
    myCars[0]="Saab";      
    myCars[1]="Volvo";
    myCars[2]="BMW";

    $.ajax({
        type: "POST",
        url: "WebService.asmx/Concat",
        data: {arr: myCars},        //can't figure out what to put here     
        success: onSuccess,
        Error: onError
    });
});

function onSuccess()
{
    alert("testing");
}

function onError() 
{
    alert("fail");
}

</script>

Any help is appreciated!

任何帮助都是赞赏!

1 个解决方案

#1


21  

Revised server-side code:

修改服务器端代码:

[WebMethod]
public string Concat(List<string> arr)
{
    string result = "";
    for (int i = 0; i < arr.Count; i++)
    {
        result += arr[i];
    }
    return result;
}

Also, add this above your WebService class declaration:

另外,在WebService类声明中添加以下内容:

[System.Web.Script.Services.ScriptService]

Revised client-side code:

修改客户端代码:

    $(document).ready(function () {

        var myCars = new Array();
        myCars[0] = "Saab";
        myCars[1] = "Volvo";
        myCars[2] = "BMW";

        $.ajax({
            type: "POST",
            url: "WebService.asmx/Concat",
            data: JSON.stringify({ arr: myCars }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: onSuccess,
            failure: onError
        });
    });

    function onSuccess(response) {
        alert(response.d);
    }

    function onError() {
        alert("fail");
    }

Also, add above that script block a reference to JSON2, such as:

此外,在该脚本之上添加对JSON2的引用,例如:

<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>

Notes:

注:

  • I have tested this under .NET 4 and using jQuery 1.6.4.
  • 我已经在。net 4和jQuery 1.6.4下测试过了。
  • Make sure you keep the client and server variable names in sync:
    public string Concat(List<string> arr)
    data: JSON.stringify({ arr: myCars })
  • 确保客户端和服务器变量名保持同步:public string Concat(列表 arr)数据:JSON。stringify({加勒比海盗:myCars })

#1


21  

Revised server-side code:

修改服务器端代码:

[WebMethod]
public string Concat(List<string> arr)
{
    string result = "";
    for (int i = 0; i < arr.Count; i++)
    {
        result += arr[i];
    }
    return result;
}

Also, add this above your WebService class declaration:

另外,在WebService类声明中添加以下内容:

[System.Web.Script.Services.ScriptService]

Revised client-side code:

修改客户端代码:

    $(document).ready(function () {

        var myCars = new Array();
        myCars[0] = "Saab";
        myCars[1] = "Volvo";
        myCars[2] = "BMW";

        $.ajax({
            type: "POST",
            url: "WebService.asmx/Concat",
            data: JSON.stringify({ arr: myCars }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: onSuccess,
            failure: onError
        });
    });

    function onSuccess(response) {
        alert(response.d);
    }

    function onError() {
        alert("fail");
    }

Also, add above that script block a reference to JSON2, such as:

此外,在该脚本之上添加对JSON2的引用,例如:

<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>

Notes:

注:

  • I have tested this under .NET 4 and using jQuery 1.6.4.
  • 我已经在。net 4和jQuery 1.6.4下测试过了。
  • Make sure you keep the client and server variable names in sync:
    public string Concat(List<string> arr)
    data: JSON.stringify({ arr: myCars })
  • 确保客户端和服务器变量名保持同步:public string Concat(列表 arr)数据:JSON。stringify({加勒比海盗:myCars })