开发者

Does a WebMethod always return XML?

ASP.NET [WebMethod], does it always return XML开发者_如何学C?

I know it can only return serializable data types however can it for instance return a JSON?


As I'm aware of, you can return XML or JSON.

To return JSON add this annotation or your method:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

And on your class allow ScriptService

[ScriptService]

An example:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Dictionary<string, object> Test()
{
    var ret = new Dictionary<string, object>();
    ret.Add("Test", 1);
    return ret;
}

// result:
{d:{Test:1}}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜