2
在ExtJS 4.2.2中通過Ajax調用從asp.net C#中的Webmethod傳輸JSON數據時,會在字符串的開頭和末尾添加幾個字符。從C#傳輸到ExtJS時JSON數據更改
JSON數據之前留下C#:
[{"ID":"0","NAME":"ALAN"},{"ID":"1","NAME":"BLAKE"}]
JSON數據通過由ExtJS的
{"d":"[{"ID":"0","NAME":"ALAN"},{"ID":"1","NAME":"BLAKE"}]"}
這也將發生,如果JSON數據具有一組根屬性接收螢火所見。 從它看起來好像沿線的某處將傳入數據視爲JSON字符串中的變量或類似的東西。在C#結束
代碼:
[WebService(Namespace = "localhost")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class Director : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString = false)]
public string getData()
{
string json = "[{\"ID\":\"0\",\"NAME\":\"ALAN\"},{\"ID\":\"1\",\"NAME\":\"BLAKE\"}]";
System.Diagnostics.Debug.WriteLine(json);
return json;
}
}
代碼ExtJS的Ajax調用(已經實施的解決方法):
Ext.Ajax.request({
async: false,
url: Test061014.ApplicationPath + '/Director.asmx/getData',
headers: { 'Content-Type': 'application/json' },
scope: this,
success: function (conn, response, options, eOpt) {
var s = conn.responseText;
s = s.substring(6, (s.length - 2));
s = s.replace(/\\/g, "");
categoryData = JSON.parse(s);
},
});
感謝您的幫助 – Mike