来自ASP.NET的多个返回值的JQuery Ajax

Rod*_*igo 2 asp.net ajax jquery webmethod

如何在成功函数中从JQuery.Ajax()返回多个值?

我尝试过这个:

          $.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]);
                }
            });
Run Code Online (Sandbox Code Playgroud)

在这里我的ASP.NET页面:

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

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

     Return a

End Function
Run Code Online (Sandbox Code Playgroud)

它只在唯一的返回字符串中工作,但数组不起作用:(

SLa*_*aks 8

将其更改为msg.d[0].

怀孕msg.a是完全错误的; 方法的返回值与变量名无关.

但是,出于安全原因,ASP.Net将JSON对象包装在d属性中,因此您需要编写msg.d以访问该数组.