Web服务返回json [英] Web service returning json

查看:94
本文介绍了Web服务返回json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试从Web服务以json的形式返回.代码为

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

推荐答案

.ajax({ 类型:' 发布', url:" , contentType:" , dataType:" , 成功:函数(数据){console.log(data); }, 错误:功能(错误,状态){ console.log(" + status); console.log(错误); } }); 但是我遇到一个错误 " http:// www.w3.org/2003/05/soap-envelope"xmlns:xsi =" http://www.w3.org/2001/XMLSchema-实例" 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 ="zh-CN"> System.Web.Services.Protocols.SoapException:服务器无法处理请求. ---> System.Xml.XmlException:根元素丢失.System的System.Xml.XmlTextReaderImpl.Read()的System.Xml.XmlTextReaderImpl.ParseDocumentContent()处的System.Xml.XmlTextReaderImpl.Throw(Exception e) System.Xml.XmlReader.MoveToContent()(位于System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()位于.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read() System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()的rotocols.SoapServerProtocolHelper.GetRequestElement()在System.Web.Services.Protocols.SoapServerProizecol(In. ),位于System.Web.Services.Protocols.ServerProtocolFactory.Create(类型类型,HttpContext上下文,HttpRequest请求,HttpResponse响应,Boolean& ---内部异常堆栈跟踪的结尾---</soap:text></soap:reason>< soap:detail/</soap:fault></soap:body> </soap:envelope>" 为什么会发生
.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


该错误表示它的意思.根元素丢失.我想这意味着您要创建的XML具有多个根.我不知道JavaScriptSerializer是什么,但是您可能要查看它返回的内容,并查看它是否是您想要的远程内容.
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.


System.Xml. XmlException:根元素丢失.
都说了.

查看您的XML文件格式是否正确.确保根标记之前的顶部没有空格或换行.
流的位置似乎不在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?


这篇关于Web服务返回json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆