【问题标题】:{"d": null} showing after JSON response from ASMX Web Service{"d": null} 在来自 ASMX Web 服务的 JSON 响应之后显示
【发布时间】:2018-01-30 21:45:44
【问题描述】:

我一直在搜索和阅读并尝试了许多不同的变体,以确保讨厌的 {"d": null} 不会附加到我的 JSON 响应的末尾,但是,我不知所措。我有一个使用 Context.Response.Write 方法的 .asmx Web 服务,如下所示:

DataTable products = my_source;

string jsonResponse = JsonConvert.SerializeObject(products);//, Formatting.None, new JsonSerializerSettings { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore} );

this.Context.Response.Clear();
this.Context.Response.ContentType = "application/json; charset=utf-8";

this.Context.Response.Write(jsonResponse);
HttpContext.Current.ApplicationInstance.CompleteRequest();

我尝试在写入后也使用 Flush(),但没有成功。这只会返回 JSON,但也会返回一个错误,指出在发送标头后无法刷新。我使用了 Write.End() 而不是 CompleteRequest,这也会从 JSON 响应中截断 ]。我将返回一个字符串,而不是使用 Context.Response.Write,但是,这也会在 JSON 响应的末尾去掉 ] 括号。

我的方法属性如下:

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]

我正在使用简单的 ajax 请求调用服务:

$.ajax({
    url : '/ProductCatalog/ProductMasterWS.asmx/GetProducts',
    type : 'GET',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (data) {
        populate(data);
    },
    error: function(xhr, ajaxOptions, thrownError) {
        console.log(xhr.status + " " + thrownError.stack);
    }
});

非常感谢任何想法/建议!

【问题讨论】:

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


    【解决方案1】:

    在服务器端试试这个解决方案:

    [WebMethod]
    public void Select()
    {
        String result = String.Empty;
        List<UserRole> userRoles = new UserRoleController().Select(new UserRole());
        JavaScriptSerializer js = new JavaScriptSerializer();
        result = js.Serialize(userRoles);
    
        Context.Response.Clear();
        Context.Response.ContentType = "application/json; charset=utf-8";
        Context.Response.Write(result);
    }
    

    【讨论】:

      【解决方案2】:

      我知道这是一个非常古老的问题,但我在使用 IIS 时遇到了同样的问题(在 Visual Studio 中调试不会在响应中添加 '"d":null')。

      就我而言,我通过修改应用程序池解决了这个问题。默认情况下,新站点会创建一个使用 .NET CLR v4.0.30319 的新应用程序池(我使用的是 IIS v10.0.18362.1)

      .NET CLR 版本应为 v2.0.50727(集成模式)

      【讨论】:

        【解决方案3】:

        试试这个:

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/json";
        HttpContext.Current.Response.AddHeader("content-length", res.Length.ToString());
        HttpContext.Current.Response.Write(YOUR_JSON_RESPONSE);
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
        

        【讨论】:

        • 欢迎来到 StackOverflow。请解释您的答案,而不仅仅是发布随机代码。
        【解决方案4】:

        不要那样实现你的 web 服务,只返回一个包含你从 web 方法的响应的 POCO 对象。 ASMX 会为您处理剩下的事情。

        【讨论】:

        • 当我返回一个字符串时,ASP.NET 会自动在我的响应周围添加这个 tempuri.org">。我需要一个普通的 JSON 响应。我怎样才能做到这一点?
        • 返回一个 POCO 对象怎么样?当您将响应设置为 JSON 时,它应该会自动序列化。
        • 我尝试返回一个 List,但是,它不会序列化为 JSON 对象,并且会使返回的内容进一步复杂化。 w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema" xmlns="tempuri.org/">...</ArrayOfPocoModel>
        猜你喜欢
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        • 2012-08-24
        • 1970-01-01
        • 2011-10-19
        • 2012-07-04
        • 2011-02-10
        • 1970-01-01
        相关资源
        最近更新 更多