asp.net-mvc – ASP.NET MVC JsonResult返回500

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – ASP.NET MVC JsonResult返回500前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个控制器方法
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) }
        });
    });

我总是收到错误代码500.问题是什么?

谢谢

解决方法

如果你看到实际的反应,它可能会说

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);
}
原文链接:/aspnet/251087.html

猜你在找的asp.Net相关文章