Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET service Web
Hi
 
I am trying to return as json from web service .here is the code
 
[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 18-Jul-12 21:02pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
mphotstuff at 19-Jul-12 2:10am
   
I am passing json value from web service not xml
Christian Graus at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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?
  Permalink  
v2
Comments
Christian Graus at 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)

  Print Answers RSS
0 OriginalGriff 7,205
1 DamithSL 5,114
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,747
4 Kornfeld Eliyahu Peter 4,474


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 19 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100