开发者

JQuery Ajax with multiple return values from ASP.NET

How can I return multiple values from JQuery.Ajax() in the success function ?

I tried it:

          $.ajax({
                type: "POST",
              开发者_开发技巧  url: "default.aspx/myFunction",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    $("#myDiv").html(msg.a[0]);
                    $("#myDiv2").html(msg.a[1]);
                }
            });

And here my ASP.NET page:

<WebMethod()> _
Public Shared Function myFunction() As Array

     Dim a() As String
     a(0) = "value 1"
     a(1) = "value 2"

     Return a

End Function

It works just in unique return string, but array doesn't work :(


Change it to msg.d[0].

Wirting msg.a is completely wrong; the method's return value has nothing to do with a variable name.

However, for security reasons, ASP.Net wraps the JSON object in a d property, so you need to write msg.d to access the array.


For Myself I used response.d to return the full array formatted in Comma's ex.[1,2,3]. To receive an individual, it's the same as the above (response.d[0]). The values were returned to AJAX as List(Of Strings) using a WebMethod.

                var Str = {};
                Str.StrVal = 'Test1';
                Str.StrVal2 = 'Test2';
                Str.StrVal3 = 'Test3';
                Str.StrVal4 = 'Test4';
                $.ajax({
                    type: "POST",
                    url: "VB.aspx/GetString",
                    data: '{Str: ' + JSON.stringify(Str) + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert("User has been added successfully." + response.d);
                        //window.location.reload();
                        //window.data("kendoWindow").close();
                        Str.StrVal = response.d[0]//response.d
                        //AddTab(response.d)
                        GetTabs(Str.StrVal)
                    },
                    error: function (response) {
                        alert(response.d)
                    }
                });

Here is the Web Method, Sorry for the brief Variable Description's. Just a sample.

<WebMethod()> _
<ScriptMethod()> _
Public Shared Function GetString(Str As SendString) As List(Of String)
    Dim a As New List(Of String)
    a.Add("1")
    a.Add("2")
    Return a
End Function

Thanks


I got the solution!

I just use msg.d[0] instead msg.a[0]

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜