2

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

1
  • 1
    Is your WebMethod actually returning a json result? Based on the code you posted, I don't think it is. Commented Oct 13, 2010 at 17:38

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

I got the solution!

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.