0
我正在使用Extjs 4.1和asp.net 我試圖從數據庫中填充網格面板但出現錯誤。我正在使用webservice來獲取數據。獲取錯誤(生成XML文檔時出錯)從asp.net中的extjs服務器端獲取數據
錯誤:
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()
我的web服務代碼
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = true, XmlSerializeString = false)]
public DataTable displayData() {
db obj = new db();
return obj.getCountry();
}
調用Web服務從js文件
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'
}
}
});
即使我從web服務[ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = true, XmlSerializeString = false)]
但還是同樣的錯誤刪除此行。
我沒有在我的代碼中使用XMLSerializer,那麼它也會給我錯誤,例如生成XML文檔時發生錯誤。
正如你所說我給了表名,但現在我得到錯誤像'Ext.Error:無法解析服務器返回的JSON:你試圖解碼無效的JSON字符串:<?xml version =「 1.0「encoding =」utf-8「?>' – Smily
似乎您的服務使用XML而不是JSON來響應。看到這個帖子的類似案例:http://stackoverflow.com/questions/5611134/responseformat-json-returns-xml – sphair
是的,你是對的..感謝迴應 – Smily