我有一个简单的web应用程序,它有一个HouseUnitController
,其操作返回JSONResult
。在ASP.NET中从JSON返回的未定义数据
public JsonResult GetHouseUnits()
{
var houseUnits = db.HouseUnits.Include(h => h.HouseModel).Include(h => h.Site)
.Select(h => new {
h.Block,
h.Lot,
h.HouseModel.ModelName,
h.Site.SiteName,
h.IsSold,
h.FloorArea,
h.LotArea,
h.Price
});
return Json(houseUnits, JsonRequestBehavior.AllowGet);
}
在我看来,我有:
<button data-retrieve data-view="tree" data-service = "./GetHouseUnits" data-container="#view" class="btn btn-warning btn-sm" > <span class="glyphicon glyphicon-arrow-down"></span> <strong>Load Data </strong> </button>
当我浏览localhost:62516/HouseUnits/GetHouseUnits
,我可以看到返回的JSON结果。但是,当我点击按钮时,我没有收到任何数据。这里是正在为其他HTML页面我的脚本文件:
$('button[data-retrieve]').click(function(){
var treeHeaderLimit = 2;
var APIurl = $(this).data('service');
var view = $(this).data("view");
var dataCount = 5;
var selId = $(this).data("container");
createView(APIurl, selId, view, dataCount, treeHeaderLimit);
});
我有一种感觉,它是与在按钮的URL,因为它似乎是从AJAX返回的数据是undefined
。
function createView(APIurl, selId, viewType, dataCount, treeHeaderLimit){
$.ajax({
url: APIurl,
type: "GET",
contentType: "application/json",
dataType: "json", //This is used to avoid `No-Access-Control-Allow-Origin` header error
success:function(data){
if (viewType=="tree"){
generateTree(data, selId, dataCount, treeHeaderLimit)
} else{
generateTable(data, selId, dataCount);
}
},
error: function (err) {
alert(err);
}
});
}
有人可以帮我解决这个问题吗?我试过./GetHouseUnits
和../GetHouseUnits
,但它不起作用。谢谢。
尝试使用Url.Action()代替静态URL的 –
您是否为数据服务属性尝试了'HouseUnits/GetHouseUnits'? – Sunil
谢谢@FrebinFrancis。发布这个答案,我可以接受它。 – Cyval