【问题标题】:Asp.net web service returning xml instead of json to browser using httpAsp.net Web 服务使用 http 返回 xml 而不是 json 到浏览器
【发布时间】:2016-01-11 09:20:09
【问题描述】:

我有一个简单的代码,需要使用浏览器以 json 格式返回字符串。

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string FunctiGetJsononName(string str)
    {
        JavaScriptSerializer ser = new JavaScriptSerializer();
        return ser.Serialize(str);
    }

我得到的输出是 xml 中的 json。我想要没有 XML 的 json。

我也不能使用 void 方法。 另外,我正在使用框架 4.5.2 和空模板(我不能使用其他任何东西)。

【问题讨论】:

    标签: c# asp.net json web-services


    【解决方案1】:
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    [WebMethod]
    public string  FunctiGetJsononName(string str){
    
        Context.Response.Clear();
        Context.Response.ContentType = "application/json"; 
    ........
    }
    

    试试这个...可能对你有帮助!! 它帮助了我...

    【讨论】:

    • 感谢您的评论,我试过了,我仍然得到 sme 结果。
    猜你喜欢
    • 2012-02-26
    • 2023-04-05
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    相关资源
    最近更新 更多