我不知道我錯在哪裏,我錯過了什麼。ASP.NET WebService正在用XML標記包裝我的JSON響應
我正在構建一個ASP.NET 2.0(在.Net 3.5框架)Web應用程序,我包括一個web服務。請注意,這是而不是的MVC項目。我希望公開一個將返回一個JSON字符串的方法;格式化爲jqGrid jQuery插件。
這是初步的測試方法,我在我的服務中實現:由於(Phil Haack's Guide for MVC)
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getData()
{
JavaScriptSerializer ser = new JavaScriptSerializer();
var jsonData = new
{
total = 1, // we'll implement later
page = 1,
records = 3, // implement later
rows = new[]{
new {id = 1, cell = new[] {"1", "-7", "Is this a good question?", "yay"}},
new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?", "yay"}},
new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?", "yay"}}
}
};
return ser.Serialize(jsonData); //products.ToString();
}
當調用此函數返回(格式爲清楚起見):
<?xml version="1.0" encoding="utf-8" ?>
<string mlns="http://tempuri.org/">
{
"total":1,
"page":1,
"records":3,
"rows":
[
{"id":1,"cell":["1","-7","Is this a good question?","yay"]},
{"id":2,"cell":["2","15","Is this a blatant ripoff?","yay"]},
{"id":3,"cell":["3","23","Why is the sky blue?","yay"]}
]
}
</string>
會如何我實現了上述迴應沒有的xml包裝?
你是如何得到結果的。當我像你一樣執行時,我似乎只返回「[object Object]」。這可能是JSON天真,但我似乎無法得到它的工作。 – Mike 2010-01-14 09:35:10
我在Firefox中使用firebug查看網絡面板的響應 - 單擊該請求的響應選項卡。 – 2010-01-14 09:59:40
你知道爲什麼數據被包裹在變量「d」中嗎? – Mike 2010-01-14 10:07:08