C# Can';t从.net中的JSON响应中删除.d封装
即使在写了这篇很棒的博文之后,我也无法逃避JSON响应中的.d封装。我不知道我是否做错了什么,所以我将复制服务器端和客户端代码 我正在使用Newtonsoft.JSON库序列化JSON 客户端:C# Can';t从.net中的JSON响应中删除.d封装,c#,jquery,asp.net,ajax,json,C#,Jquery,Asp.net,Ajax,Json,即使在写了这篇很棒的博文之后,我也无法逃避JSON响应中的.d封装。我不知道我是否做错了什么,所以我将复制服务器端和客户端代码 我正在使用Newtonsoft.JSON库序列化JSON 客户端: <script type="text/javascript"> $(function () { $("#bt").click(function () { $.ajax({ type: "POST", url: "<
<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>
服务器端-更新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写入流尝试从ajax调用中删除
数据类型
参数。从中,请参阅“数据类型:不关你的事”@DanielJ.G。不幸的是,即使没有它,我仍然收到。为什么当我点击这篇文章中的第一个链接时会收到“此网站包含恶意软件”的警告?@Mike它一定与一些被谷歌认定为危险的恶意脚本有关。希望,网站的所有者会解决这些问题。我认为这是答案,看起来我们走在一条很好的道路上,但这并不能解决我的问题。实际上,我得到的是无效的JSON。JSON输出可以在这里看到,您如何使用System.Web.Script.Serialization.JavaScriptSerializer创建JSON?是的,我举了您的确切示例,它按预期删除了.d,但JSON无效。只需将吸引力传递给Serialize(),我不认为需要Jobject,Jarray无论何时访问克罗地亚,请告诉我,我欠你一杯啤酒!:)谢谢,将景点直接传递给对象就是解决方案。
[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();
}