ASP.NET MVC JsonResult return 500

后端 未结 2 704
孤独总比滥情好
孤独总比滥情好 2021-01-17 20:44

I have this controller method:

public JsonResult List(int number) {
 var list = new Dictionary  ();

 list.Add(1, \"one\");
 list.Add(2, \         


        
2条回答
  •  醉梦人生
    2021-01-17 21:32

    If you saw the actual response, it would probably say

    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.

    You'll need to use the overloaded Json constructor to include a JsonRequestBehavior of JsonRequestBehavior.AllowGet such as:

    return Json(list, JsonRequestBehavior.AllowGet);
    

    Here's how it looks in your example code (note this also changes your ints to strings or else you'd get another error).

    public JsonResult List(int number) {
      var list = new Dictionary();
    
      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);
    }
    

提交回复
热议问题