开发者

Does a WebMethod always return XML?

开发者 https://www.devze.com 2023-01-19 09:21 出处:网络
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.

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

精彩评论

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