如何让 ASMX 文件输出 JSON [英] How to let an ASMX file output JSON

查看:52
本文介绍了如何让 ASMX 文件输出 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有代码隐藏文件的 ASMX 文件.它工作正常,但输出的是 XML.

I created an ASMX file with a code behind file. It's working fine, but it is outputting XML.

但是,我需要它来输出 JSON.ResponseFormat 配置似乎不起作用.我的代码隐藏是:

However, I need it to output JSON. The ResponseFormat configuration doesn't seem to work. My code-behind is:

[System.Web.Script.Services.ScriptService]
public class _default : System.Web.Services.WebService {
    [WebMethod]
    [ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
    public string[] UserDetails()
    {
        return new string[] { "abc", "def" };
    }
}

推荐答案

来自 WebService 返回 XML即使 ResponseFormat 设置为 JSON:

确保请求是 POST 请求,而不是 GET.Scott Guthrie 有一个 解释原因的帖子.

Make sure that the request is a POST request, not a GET. Scott Guthrie has a post explaining why.

虽然它是专门为 jQuery 编写的,但它也可能对您有用:
使用jQuery 使用 ASP.NET JSON Web 服务

Though it's written specifically for jQuery, this may also be useful to you:
Using jQuery to Consume ASP.NET JSON Web Services

这篇关于如何让 ASMX 文件输出 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆