jquery - Send JSON data to a WebMethod -


i'm trying post data 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 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 this:

<webmethod()> _ public function savesocialloginkeys(byval myrows string) string     dim response string = ""     '------------some code here-------------------------------     '------------response based on results per code-------     return response end function 

when tried debug, ajax call showing error!

the ajax call failing because you're sending json object webmethod, webmethod accepts string. asp.net trying smart , convert stringified json object real object, time request gets webmethod, it's no longer string.

you need simple object represents json structure:

public class socialconnectionmodel     public property uniqueid string      public property description string      public property clientid string      public property secretkey string      public property active string      public property delete string end class 

since json object contains array of objects under myrows key, that's 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 socialconnectionmodel()) boolean         ' data          ' return true if succeeded         return true     end function  end class 

if haven't done already, including <scriptservice()> line important since calling webmethod ajax.

now ajax call should work 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) {         // code here     },     error: function (response) {         // code here     } }); 

a suggestion: try build json data appropriate types, instead of strings every property. example, uniqueid property should integer, , active property bool (i'm guessing). don't make stringly typed unless have to. :)


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)