Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am trying to return as json from web service .here is the code

C#
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
      public string GetData()
      {
         var List = Data.ToList();
          return new JavaScriptSerializer().Serialize(List);
      }

And my code in Client is


  $.ajax({
            type: 'Post',
            url: "http://localhost/service/WebService.asmx?op=Method",
            contentType: "application/json",
            dataType: "json",
            success: function (data) { console.log(data); },
            error: function (error, status) {
                console.log("status = " + status);
                console.log(error);
            }
        });

But I am getting an error


"<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 was unable to process request. ---> System.Xml.XmlException: Root element is missing.   at System.Xml.XmlTextReaderImpl.Throw(Exception e)   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()   at System.Xml.XmlTextReaderImpl.Read()   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()   at System.Xml.XmlReader.MoveToContent()   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()   at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()   at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()   at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()   at 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>"


Why this happens
Posted

The error means what it says. The root element is missing. I guess that means the XML you're creating has more than one root. I don't know what JavaScriptSerializer is, but you may want to look at what it returns and see if it's remotely what you want.
 
Share this answer
 
Comments
Jameel VM 19-Jul-12 2:10am    
I am passing json value from web service not xml
Christian Graus 19-Jul-12 2:22am    
Do you understand what a webservice is ? Webservices communicate using XML. The XML you're trying to send is malformed. That's what the error message means. You're using a web service, therefore you're using XML. That's why your error message talks about a problem with XML.
System.Xml.XmlException: Root element is missing.
It says all.

See if your XML file is properly formed. Make sure there is no space or new line at the top before the root tag.
Looks like stream position is not at the start of the XML making it look like root element missing.
What is there in data and how was it formed?
 
Share this answer
 
v2
Comments
Christian Graus 19-Jul-12 2:22am    
Really ? You can't have a space or line above the root tag ? Are you sure ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900