jquery - 将 JSON 数据发送到 WebMethod

标签 jquery json ajax vb.net 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

        }
    });

服务器端Web方法是这样的:

<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 调用显示错误!

最佳答案

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 下的对象数组key,这就是 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 属性可以是 bool 值(我猜)。不要什么都做stringly typed除非你必须这样做。 :)

关于jquery - 将 JSON 数据发送到 WebMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37580426/

相关文章:

javascript - 使用 Ajax 导航栏 onclick

php - 使用 AJAX(PHP 和 JQuery)聊天心跳

Javascript:无法将字符串与数字进行比较

javascript - 更新textarea上的mysql数据点击关闭

json - 在 config.json 文件中输入自定义类型值

ios - 无法填充第二个 UITableView Controller

java - 接收json数据时出现JSON解析错误

php - 如何使用 JQUERY 设置复选框选中和未选中事件

javascript - 单击时将嵌套数组/对象值显示到单独的元素中

javascript - jQuery 附加不需要的数据