Pet*_*eti 8 asp.net-mvc jquery json
我有这个控制器方法:
public JsonResult List(int number)
{
var list = new Dictionary<int, string>();
list.Add(1, "one");
list.Add(2, "two");
list.Add(3, "three");
var q = (from h in list
where h.Key == number
select new
{
key = h.Key,
value = h.Value
});
return Json(list);
}
Run Code Online (Sandbox Code Playgroud)
在客户端,有这个jQuery脚本:
$("#radio1").click(function () {
$.ajax({
url: "/Home/List",
dataType: "json",
data: { number: '1' },
success: function (data) { alert(data) },
error: function (xhr) { alert(xhr.status) }
});
});
Run Code Online (Sandbox Code Playgroud)
我总是得到一个错误代码500.问题是什么?
谢谢
Jus*_*lle 19
如果你看到实际的反应,它可能会说
此请求已被阻止,因为当在GET请求中使用敏感信息时,可能会向第三方网站公开敏感信息.要允许GET请求,请将JsonRequestBehavior设置为AllowGet.
你需要使用重载的Json构造函数包含JsonRequestBehavior的JsonRequestBehavior.AllowGet如:
return Json(list, JsonRequestBehavior.AllowGet);
Run Code Online (Sandbox Code Playgroud)
以下是它在示例代码中的显示方式(请注意,这也会将您的ints 更改为strings,否则您将收到另一个错误).
public JsonResult List(int number) {
var list = new Dictionary<string, string>();
list.Add("1", "one");
list.Add("2", "two");
list.Add("3", "three");
var q = (from h in list
where h.Key == number.ToString()
select new {
key = h.Key,
value = h.Value
});
return Json(list, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7572 次 |
| 最近记录: |