javascript - Data at the root level is invalid on $.ajax call -
i have simple web service trying utilize. obviously, more enahnced down road trying grasp basic concept of ajax call.
web service
using system; using system.collections.generic; using system.linq; using system.web; using system.web.services; namespace tools { /// <summary> /// summary description cfservices1 /// </summary> [webservice(namespace = "http://localhost:51342/")] [webservicebinding(conformsto = wsiprofiles.basicprofile1_1)] [system.componentmodel.toolboxitem(false)] // allow web service called script, using asp.net ajax, uncomment following line. // [system.web.script.services.scriptservice] public class cfservices1 : system.web.services.webservice { [webmethod] public string helloworld() { return "hellow world"; } } }
javascript
$.ajax({ type: "post", url: "cfservices.asmx?/helloworld", data: "{}", contenttype: "application/json; charset=utf-8", datatype: "json", success: function (data) { console.log(data); }, error: function (response) { console.log(response); } });
the ajax call appears work fine returns error:
"<?xml version="1.0" encoding="utf-8"?><soap:envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"><soap:body><soap:fault><soap:code><soap:value>soap:receiver</soap:value></soap:code><soap:reason><soap:text xml:lang="en">system.web.services.protocols.soapexception: server unable process request. ---> system.xml.xmlexception: data @ root level invalid. line 1, position 1. @ system.xml.xmltextreaderimpl.throw(exception e) @ system.xml.xmltextreaderimpl.throw(string res, string arg) @ system.xml.xmltextreaderimpl.parserootlevelwhitespace() @ system.xml.xmltextreaderimpl.parsedocumentcontent() @ system.xml.xmltextreaderimpl.read() @ system.xml.xmltextreader.read() @ system.web.services.protocols.soapserverprotocol.soapenvelopereader.read() @ system.xml.xmlreader.movetocontent() @ system.web.services.protocols.soapserverprotocol.soapenvelopereader.movetocontent() @ system.web.services.protocols.soapserverprotocolhelper.getrequestelement() @ system.web.services.protocols.soap12serverprotocolhelper.routerequest() @ system.web.services.protocols.soapserverprotocol.routerequest(soapservermessage message) @ system.web.services.protocols.soapserverprotocol.initialize() @ system.web.services.protocols.serverprotocolfactory.create(type type, httpcontext context, httprequest request, httpresponse response, boolean& abortprocessing) --- end of inner exception stack trace ---</soap:text></soap:reason><soap:detail /></soap:fault></soap:body></soap:envelope>"
i have tried couple different things lead me down path of url property being invalid since i'm getting actual response, i'm assuming it's correct. renders if navigate through browser. if try , invoke web service through asmx page, works.
i have tried changing data type plain text doesn't work either. since web method reutnr string 'hellow world', shouldn't need pass arguments tried passing blank values in case. either brings me response returning 'undefined', html markup asmx page or this. 'data @ root element invalid.' tells me either data being sent to, or recieved web service incorrect or doesn't have right formatting. i'm getting hung @ because can't figure out possibly wrong here.
although simple, i'm not finding luck whatsoever on sof or other threads. helpful insight appreciated.
as calling jquery ie., script part need make below changes webservice part,
[system.web.script.services.scriptservice] // mark accessing script public class cfservices1 : system.web.services.webservice
we need set script method attribute web method below
[webmethod] [scriptmethod] public string helloworld()
then while calling ajax call remove ? in url part below,
$.ajax({ type: "post", url: "cfservices.asmx/helloworld", data: "{}", contenttype: "application/json; charset=utf-8", datatype: "json", success: function (data) { console.log(data); }, error: function (response) { console.log(response); }});
hope helps.
Comments
Post a Comment