获取错误(生成 XML 文档时出错。)在 asp.net 的 extjs 中从服务器端获取数据
Posted
技术标签:
【中文标题】获取错误(生成 XML 文档时出错。)在 asp.net 的 extjs 中从服务器端获取数据【英文标题】:getting Error(There was an error generating the XML document.) fetch data from server side in extjs in asp.net 【发布时间】:2012-10-19 07:43:09 【问题描述】:我正在使用 Extjs 4.1 和 asp.net 我正在尝试从数据库中填充网格面板,但出现错误。我正在使用网络服务来获取数据。
错误:
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Cannot serialize the DataTable. DataTable name is not set.
at System.Data.DataTable.WriteXmlSchema(XmlWriter writer, Boolean writeHierarchy)
at System.Data.DataTable.System.Xml.Serialization.IXmlSerializable.WriteXml(XmlWriter writer)
at System.Xml.Serialization.XmlSerializationWriter.WriteSerializable(IXmlSerializable serializable, String name, String ns, Boolean isNullable, Boolean wrapped)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_DataTable(Object o)
at Microsoft.Xml.Serialization.GeneratedAssembly.DataTableSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
我的网络服务代码
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = true, XmlSerializeString = false)]
public DataTable displayData()
db obj = new db();
return obj.getCountry();
从 js 文件调用 web 服务
var store = Ext.create('Ext.data.Store',
pageSize: 20,
model: 'ForumThread',
autoLoad: true,
proxy:
type: 'ajax',
url: 'report.asmx/displayData',
dataType: "json",
contentType: "application/json; charset=utf-8",
//url: '/grid.json',
reader:
root: 'data' ,
type: 'json',
method: "GET",
totalProperty: 'totalCount'
);
即使我从 webservice [ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = true, XmlSerializeString = false)]
中删除了这一行,但仍然是同样的错误。
我没有在我的代码中使用 XMLSerializer,然后它也会给我错误,比如生成 XML 文档时出错。
【问题讨论】:
【参考方案1】:如错误提示,您需要为数据表提供名称。
dt.TableName = "我的表";
【讨论】:
正如你所说,我给出了表名,但现在我收到了类似Ext.Error: Unable to parse the JSON returned by the server: You're trying to decode an invalid JSON String: <?xml version="1.0" encoding="utf-8"?>
的错误
您的服务似乎使用 XML 而不是 JSON 进行响应。类似情况见此帖:***.com/questions/5611134/…以上是关于获取错误(生成 XML 文档时出错。)在 asp.net 的 extjs 中从服务器端获取数据的主要内容,如果未能解决你的问题,请参考以下文章
XML 文档中存在错误 (0, 0) - 将 XML 反序列化为对象时出错