Send JSON data to a WebMethod

I'm trying to POST some data to a VB.NET WebMethod via AJAX.

JSON.stringify(myRows) contains:

{
   "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"
      }
   ]
}

My AJAX call is:

$.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

        }
    });

Server side web method is this:

<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

When I tried to debug, AJAX call is showing error!


The AJAX call is failing because you're sending a JSON object to the WebMethod, but the WebMethod accepts a String. ASP.NET is trying to be smart and convert the stringified JSON object into a real object, so by the time the request gets to your WebMethod, it's no longer a string.

You need a simple object that represents your JSON structure:

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

Since your JSON object contains an array of objects under the myRows key, that's what the WebMethod signature must accept:

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

If you haven't done so already, including the <ScriptService()> line is important since you are calling this WebMethod from AJAX.

Now your AJAX call should work as expected:

$.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
    }
});

A suggestion: Try to build your JSON data with appropriate types, instead of strings for every property. For example, the UniqueId property should be an integer, and the Active property could be a bool (I'm guessing). Don't make everything stringly typed unless you have to. :)


你不发送一个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

                    }
                });
链接地址: http://www.djcxy.com/p/46158.html

上一篇: Ajax POST对参数[assetID]有空条目?

下一篇: 将JSON数据发送到WebMethod