将 JSON 数据发送到 WebMethod

Send JSON data to a WebMethod

我正在尝试通过 AJAX.POST 一些数据到 VB.NET WebMethod。

JSON.stringify(myRows) 包含:

{
   "myRows":[
      {
         "UniqueId":"188",
         "Description":"hello",
         "ClientId":"321",
         "SecretKey":"dftete",
         "Active":"checked",
         "Delete":"delete icon"
      },
      {
         "UniqueId":"191",
         "Description":"sfsss",
         "ClientId":"fsdfs",
         "SecretKey":"cvvcvb",
         "Active":"unchecked",
         "Delete":"delete icon"
      },
      {
         "UniqueId":"201",
         "Description":"I am test singh",
         "ClientId":"23424242",
         "SecretKey":";kfddgdfl;ghf",
         "Active":"unchecked",
         "Delete":"delete icon"
      },
      {
         "UniqueId":"202",
         "Description":"Yay mai ban ne wala hun",
         "ClientId":"n.csdvnsssl",
         "SecretKey":"nj.ssdnfvel,vgd",
         "Active":"unchecked",
         "Delete":"delete icon"
      }
   ]
}

我的 AJAX 电话是:

$.ajax({
        type: "POST",
        url: "MyWebServiceUtilities.asmx/savesocialloginkeys",
        data: JSON.stringify(myRows),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
             //some code here
        },
        failure: function (response) {
           //some code here    
        },
        error: function (response) {
            //some code here

        }
    });

服务器端网络方法是这样的:

<WebMethod()> _
Public Function savesocialloginkeys(ByVal myrows As String) As String
    Dim response As String = ""
    '------------Some code here-------------------------------
    '------------Response will be based on results as per code-------
    Return response
End Function

当我尝试调试时,AJAX 调用显示错误!

您没有向服务器发送 json 对象,因此您必须使用 'text/html' 作为 contentType,例如:

                $.ajax({
                    type: "POST",
                    url: "MyWebServiceUtilities.asmx/savesocialloginkeys",
                    data: JSON.stringify(myRows),
                    contentType: "text/html; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                         //some code here
                    },
                    failure: function (response) {
                       //some code here    
                    },
                    error: function (response) {
                        //some code here

                    }
                });

AJAX 调用失败,因为您正在向 WebMethod 发送一个 JSON 对象,但 WebMethod 接受一个字符串。 ASP.NET 正在努力将字符串化的 JSON 对象转换为真实对象,因此当请求到达您的 WebMethod 时,它不再是字符串。

您需要一个简单的对象来表示您的 JSON 结构:

Public Class SocialConnectionModel
    Public Property UniqueId As String

    Public Property Description As String

    Public Property ClientId As String

    Public Property SecretKey As String

    Public Property Active As String

    Public Property Delete As String
End Class

由于您的 JSON 对象包含 myRows 键下的对象数组,这就是 WebMethod 签名必须接受的内容:

Imports System.Web.Services
Imports System.ComponentModel

<System.Web.Script.Services.ScriptService()>
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class MyWebServiceUtilities
    Inherits System.Web.Services.WebService

    <WebMethod()>
    Public Function savesocialloginkeys(myRows As SocialConnectionModel()) As Boolean
        ' Do something with the data

        ' Return true if succeeded
        Return True
    End Function

End Class

如果您还没有这样做,那么包含 <ScriptService()> 行很重要,因为您是从 AJAX.

调用此 WebMethod

现在您的 AJAX 调用应该可以正常工作了:

$.ajax({
    type: "POST",
    url: "MyWebServiceUtilities.asmx/savesocialloginkeys",
    data: JSON.stringify(myrows),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        if (response && response.d) {
            console.log("success");
        }
    },
    failure: function (response) {
        // some code here
    },
    error: function (response) {
        // some code here
    }
});

一个建议:尝试使用适当的类型构建您的 JSON 数据,而不是每个 属性 的字符串。例如,UniqueId 属性 应该是一个整数,而 Active 属性 可能是一个布尔值(我猜)。除非万不得已,否则不要做任何事情 stringly typed。 :)

任何仍然登陆这里的人,这里是解决方案:

<WebMethod()> _
Public Function savesocialloginkeys(ByVal myRows As Object) As String
    '------------Now you can work on myRows with the help of newtonsoft library-----'


End Function