English 中文(简体)
借助 j形形形形形形色色色,使用 j子
原标题:Passing array of strings to webmethod with variable number of arguments using jQuery AJAX

我试图将一系列的参数传到C# ASP。 NET web services using jQuery Ajax. 这里是我的抽样网络方法。 注:该功能接受一定数量的参数。 当我管理 j时,我获得了500台内服务器Error。 使用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:

$(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>

任何帮助都值得赞赏。

最佳回答

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

此外,在以下表格中添加: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");
    }

此外,在上述文字中,还提及JSON2,例如:

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

注:

  • I have tested this under .NET 4 and using 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 })
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签