加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 23:56:06 所属栏目:asp.Net 来源:网络整理
导读:我有这个控制器方法: public JsonResult List(int number){ var list = new Dictionaryint,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.Val
我有这个控制器方法:
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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读