我想在ASP.NET MVC 3.0中使用jQuery.getJSON函数,所以我编写了下面的代码进行测试:
<input id="btn" type="button" />
<script>
$("#btn").click(function () {
$.getJSON("/Location/GetData", null, function (data) {
alert(data);
});
});
</script>
我有一个具有以下方法的 LocationController:
public JsonResult GetData()
{
List<int> result = new List<int>(){1, 4, 5};
return Json(result);
}
但是这不起作用! GetData 方法调用,但未显示“警报”!
最佳答案
您需要告诉 MVC 允许通过 GET 调用您的 JSON 操作,方法是将您的返回值更改为:
return Json(result, JsonRequestBehavior.AllowGet);
默认情况下( for security reasons )它们只允许通过 POST 请求 Json。
关于asp.net-mvc - 在 .NET MVC 3.0 中使用 $.getJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6196907/