当我控制台记录
我的响应
时,我得到的只是HTML
。如何获取玩家对象
?
Site.JS:
$(".AddToPreRank").click(function (e) {
e.preventDefault();
//grab id
var id = $(this).get(0).id;
//append player to prerank list
$.ajax({
url: '@Url.Action("AddToPreRank")',
type: 'POST',
data: { id : id },
success: function (response) {
console.log(response);
alert("hello");
}
});
});
LeagueController.cs:
[HttpPost]
public ActionResult AddToPreRank(int id){
Player player= new Player();
player = db.Players.Find(id);
return Json(player);
}
最佳答案
您正在调用一个 ActionResult 方法,该方法将返回比您想要的 JSON 更多的内容。
将代码更改为
public JsonResult AddToPreRank(int id){
Player player= new Player();
player = db.Players.Find(id);
return Json(player);
}
您可能还需要确认 JavaScript 文件中获取的 URL 是否正确。参数未正确传递,或者 Razor 未正确识别 @ 转义字符。
关于c# - 如何从 ASP.NET MVC 4 中的 Controller 返回 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23233992/