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

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

[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 returns XML even when ResponseFormat set to JSON开始:

确保请求是POST请求,而不是GET请求.斯科特·古思里有post explaining why分.

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

Json相关问答推荐

Jolt转换,如果任何字段为空,则将对象值设置为空

Golang JSON Date Tim.Date()测试请求

替换字符串中特殊字符的Jolt变换

使用更高级别架构中的字段值在$def内实现约束

Flutter -控制器问题-JSON API

如何让JSON子查询在没有行的情况下返回空数组而不是NULL

当由.sh脚本执行时,AWS查询字符串不会提取任何数据

如何使用 jq 在连续的 json 记录流上调用操作

是否可以在有条件的情况下将 json 对象转换为 JOLT 中的数组?

如何在 Postman 中匹配 json 响应中的内容?并可视化

Oracle Apex - 将 JSON 对象分配给变量以返回

Go - JSON 验证抛出错误,除非我在 struct 中使用指针.为什么?

解析 JSON API 响应

hook到 Decodable.init() 以获得未指定的键?

如何使用 LINQ 在 C# 中重构对象?

在 JSON 字符串中react i18n 换行符

使用 c# 通用地展平 Json

Android JSON 库的性能和可用性比较

jQuery fullcalendar 发送自定义参数并使用 JSON 刷新日历

Python 到 JSON 序列化在十进制上失败