views:

32

answers:

2

Hi,

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

+3  A: 

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.

SLaks
A: 

I got the solution!

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

Rodrigo