日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

c# – 無法從.net中的JSON響應中刪除.d封裝

 印度阿三17 2019-07-15

即使在Never worry about ASP.NET AJAX’s .d again這篇偉大的博客文章之后,我也無法逃避我的JSON響應中的.d封裝.我不知道我是否做錯了所以我會復制我的服務器端和客戶端代碼.

我正在使用Newtonsoft.JSON庫序列化JSON.

客戶端:

<script type="text/javascript">
$(function () {
    $("#bt").click(function () {
        $.ajax({
            type: "POST",
            url: "<%= Page.ResolveUrl("~/MapView.aspx/GetLocations")%>",
            data: "{ type: '<%= Page.RouteData.Values["type"].ToString() %>', id: '<%= Page.RouteData.Values["id"].ToString() %>' }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            dataFilter: function(data) {
                var msg;

                if (typeof (JSON) !== 'undefined' && 
                    typeof (JSON.parse) === 'function')
                    msg = JSON.parse(data);
                else
                    msg = eval('('   data   ')');

                if (msg.hasOwnProperty('d'))
                    return msg.d;
                else
                    return msg;
            },
            success: function (msg) {
                console.log(msg);
            },
        });
    });
});
</script>

服務器端

    [System.Web.Services.WebMethod]
    public static string GetLocations(int id, string type)
    {
        string data = "";


        if (type == "attraction")
        {
            var attractions = db.Attraction
                                .Where(a => a.AttractionId == id)
                                .Select(p => new { p.AttractionId, p.Name, p.Description, p.Location })
                                .ToList();


            JObject attractionsJSON = new JObject(
                new JProperty("Attractions", new JArray(
                    from a in attractions
                    select new JObject(
                        new JProperty("id", a.AttractionId),
                        new JProperty("name", a.Name),
                        new JProperty("location", a.Location),
                        new JProperty("description", a.Description)
                        ))));

            data = attractionsJSON.ToString();
        }
        else if (type == "category")
        {
            var attractions = db.Attraction

                .Select(p => new { p.AttractionId, p.Name, p.Description, p.Location, p.CategoryId })
                .ToList();

            if (id != 0)
            {
                attractions = attractions.Where(a => a.CategoryId == id)
                    .ToList();
            }


            JObject attractionsJSON = new JObject(
                new JProperty("Attractions", new JArray(
                    from a in attractions
                    select new JObject(
                        new JProperty("id", a.AttractionId),
                        new JProperty("name", a.Name),
                        new JProperty("location", a.Location),
                        new JProperty("description", a.Description)
                        ))));

            data = attractionsJSON.ToString();

        }
        return data;
    }

Serverside – 更新1

    [System.Web.Services.WebMethod]
    public static void GetLocations(int id, string type)
    {
        string data = "";


        if (type == "attraction")
        {
            var attractions = db.Attraction
                                .Where(a => a.AttractionId == id)
                                .Select(p => new { p.AttractionId, p.Name, p.Description, p.Location })
                                .ToList();


            JArray attractionsJSON = new JArray(
                    from a in attractions
                    select new JObject(
                        new JProperty("id", a.AttractionId),
                        new JProperty("name", a.Name),
                        new JProperty("location", a.Location),
                        new JProperty("description", a.Description)
                        ));

            data = attractionsJSON.ToString();
        }
        else if (type == "category")
        {
            var attractions = db.Attraction

                .Select(p => new { p.AttractionId, p.Name, p.Description, p.Location, p.CategoryId })
                .ToList();

            if (id != 0)
            {
                attractions = attractions.Where(a => a.CategoryId == id)
                    .ToList();
            }


            JArray attractionsJSON = new JArray(
                    from a in attractions
                    select new JObject(
                        new JProperty("id", a.AttractionId),
                        new JProperty("name", a.Name),
                        new JProperty("location", a.Location),
                        new JProperty("description", a.Description)
                        ));

            data = attractionsJSON.ToString();

        }


        var js = new System.Web.Script.Serialization.JavaScriptSerializer();

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
        HttpContext.Current.Response.Write(js.Serialize(data));
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }
}

解決方法:

嘗試使用void方法,不返回.而是自己寫輸出來回應!

  [System.Web.Services.WebMethod]
    public static void GetLocations(int id, string type)
    {
     // var attractions = something ;

     var js = new System.Web.Script.Serialization.JavaScriptSerializer();

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
        HttpContext.Current.Response.Write(  js.Serialize(attractions )  );
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
   }

保持這兩個電話結束非常重要:

flush:確保將輸出寫入流

結束:結束流以防止asp.Net將空Json寫入流

來源:https://www./content-1-328801.html

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多