【问题标题】:Send JSON data to a WebMethod将 JSON 数据发送到 WebMethod
【发布时间】:2016-06-01 23:34:15
【问题描述】:

我正在尝试通过 AJAX 将一些数据发布到 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 调用显示错误!

【问题讨论】:

  • contentType 是您要发送的数据类型,在这种情况下您应该有:contentType: "charset=UTF-8" check stackoverflow.com/questions/18701282/…
  • 欢迎来到 StackOverflow!您应该始终发布您在调试时看到的任何错误,以便其他人可以了解发生了什么问题。

标签: jquery json ajax vb.net webmethod


【解决方案1】:

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

如果您还没有这样做,包括 &lt;ScriptService()&gt; 行很重要,因为您是从 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。 :)

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您没有向服务器发送 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
      
                          }
                      });
      

      【讨论】:

      • 我已更改 contentType,但错误仍未解决。
      • 您能发布显示您的 ajax 请求的错误吗?这将帮助我们了解问题所在
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 2016-11-11
      • 2011-09-18
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      相关资源
      最近更新 更多