我有这个控制器方法:
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); }
在客户端,有这个jQuery脚本:
$("#radio1").click(function () { $.ajax({ url: "/Home/List",dataType: "json",data: { number: '1' },success: function (data) { alert(data) },error: function (xhr) { alert(xhr.status) } }); });
谢谢
解决方法
如果你看到实际的反应,它可能会说
This request has been blocked because
sensitive information could be
disclosed to third party web sites
when this is used in a GET request. To
allow GET requests,set
JsonRequestBehavior to AllowGet.
您需要使用重载的Json构造函数来包含JsonRequestBehavior.AllowGet的JsonRequestBehavior,例如:
return Json(list,JsonRequestBehavior.AllowGet);
以下是它在示例代码中的外观(注意,这也会将您的内容更改为字符串,否则您将收到另一个错误).
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); }