简体   繁体   English

ASP.NET MVC JsonResult 返回 500

[英]ASP.NET MVC JsonResult return 500

I have this controller method:我有这个控制器方法:

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);
}

On the client side, have this jQuery script:在客户端,有这个 jQuery 脚本:

$("#radio1").click(function() {
  $.ajax({
    url: "/Home/List",
    dataType: "json",
    data: {
      number: '1'
    },
    success: function(data) {
      alert(data)
    },
    error: function(xhr) {
      alert(xhr.status)
    }
  });
});

I always get an error code 500. What's the problem?我总是收到错误代码 500。这是什么问题?

Thank you谢谢

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.此请求已被阻止,因为在 GET 请求中使用此请求时,敏感信息可能会泄露给第三方网站。 To allow GET requests, set JsonRequestBehavior to AllowGet.要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。

You'll need to use the overloaded Json constructor to include a JsonRequestBehavior of JsonRequestBehavior.AllowGet such as:您需要使用重载的Json构造函数来包含JsonRequestBehaviorJsonRequestBehavior.AllowGet例如:

return Json(list, JsonRequestBehavior.AllowGet);

Here's how it looks in your example code (note this also changes your int s to string s or else you'd get another error).这是它在您的示例代码中的外观(请注意,这也会将您的int s 更改为string s 否则您会收到另一个错误)。

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);
}

While JustinStolle's answer solves your problem, I would pay attention to the error provided from the framework.虽然 JustinStolle 的回答解决了您的问题,但我会注意框架提供的错误。 Unless you have a good reason to want to send your data with the GET method, you should aim to send it with the POST method.除非您有充分的理由想要使用GET方法发送数据,否则您应该以使用POST方法发送数据为目标。

The thing is, when you use the GET method, your parameters gets added to your request url instead of added to the headers/body of your request.问题是,当您使用GET方法时,您的参数会添加到您的请求 url 中,而不是添加到您的请求的标头/正文中。 This might seem like a tiny difference, but the error hints why it's important.这似乎是一个微小的差异,但错误提示了为什么它很重要。 Proxy servers and other potential servers between the sender and the receiver are prone to logging the request url and often ignore the headers and/or body of the request.发送方和接收方之间的代理服务器和其他潜在服务器容易记录请求 url,并且经常忽略请求的标头和/或正文。 This information is also often regarded as non important/secret so any data exposed in the url is much less secure by default.此信息通常也被视为不重要/秘密,因此默认情况下,url 中公开的任何数据都不太安全。

The best practice is then to send your data with the POST method so your data is added to the body instead of the url.最佳做法是使用POST方法发送数据,以便将数据添加到正文而不是 url。 Luckily this is easily changed, especially since you're using jquery.幸运的是,这很容易改变,尤其是当您使用 jquery 时。 You can either use the $.post wrapper or add type: "POST" to your parameters:您可以使用$.post包装器或将 type: "POST" 添加到您的参数中:

$.ajax({
            url: "/Home/List",
            type: "POST",
            dataType: "json",
            data: { number: '1' },
            success: function (data) { alert(data) },
            error: function (xhr) { alert(xhr.status) }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM