即使在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
|