开发者

Ajax callback with 2 parameters (int, List)

Working with jquery.ajax for first time...

I have a class in C#:

public class myValues
{
    string Value1 { get; set; }
    string Value2 { get; set; }
}

Then, my method is as follows:

[WebMethod]
public static string MyMethod(List<myValues> levels)
{
         //loop levels here
}

Now, in client (javascript, jquery) I'm doing an ajax' callback (sending the items within a select):

var levels = [];
//get items from select
$("#Select1 option").each(function () {

            levels.push({
                    Value1: $(this).val(),
                    Value2: $(this).text()
            });

        });

        //with json2.js :
        var jsonText = JSON.stringify({ levels: levels });
        $.ajax({
             type: "POST",
             url: "myPage.aspx/MyMethod",
             data: jsonText,
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: function () { alert("it worked"); },
             failure: function () { alert("Uh oh"); }
        });

Everything works perfectly fine here...

But now, I want to change my webmethod to receive a second parameter:

[WebMethod]
public static string MyMethod(List<myValues> levels, int id)
{
     开发者_JAVA百科    //loop levels here
}

I don't know how to call the method from the client, sending 2 parameters..?


You need to json stringify the id also, for example:

var jsonText = JSON.stringify({ levels: levels, id : 1});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜