gpt4 book ai didi

c# - 无法从 .net 中的 JSON 响应中删除 .d 封装

转载 作者:行者123 更新时间:2023-11-30 13:22:22 25 4
gpt4 key购买 nike

即使在伟大的博文之后 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;
}

服务器端 - 更新 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();
}

在最后保持这 2 个调用非常重要:

flush : 确保输出被写入流

End : 结束流,防止asp.Net向流写入空Json

关于c# - 无法从 .net 中的 JSON 响应中删除 .d 封装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25259142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com