【问题标题】:MVC (Razor) Json error (500 internal server error)MVC (Razor) Json 错误(500 内部服务器错误)
【发布时间】:2024-01-23 03:17:01
【问题描述】:

控制器动作是:

    public ActionResult GetStatesByCountry(string countryCode)
    {
        return Json(DropDownHelper.GetState(countryCode).Select(x => new { value = x.Code, text  x.Name }));
    }

当我使用 firebug 调试时,出现以下错误。

此请求已被阻止,因为在 GET 请求中使用敏感信息可能会泄露给第三方网站。要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。

【问题讨论】:

  • 调试器对错误有什么看法?
  • 它说允许对 JSON 的 GET 请求。我不知道该怎么做。

标签: asp.net-mvc razor


【解决方案1】:

使用 JsonRequestBehavior.AllowGet

return Json(DropDownHelper.GetState(countryCode).Select(x => new { value = x.Code, text = x.Name }), JsonRequestBehavior.AllowGet);

【讨论】:

    【解决方案2】:

    你必须在方法中添加JsonRequestBehavior.AllowGet,因为默认值是DenyGet

    public ActionResult GetStatesByCountry(string countryCode)
    {
        return Json(DropDownHelper.GetState(countryCode).Select(x => new { value = x.Code, text  x.Name }), JsonRequestBehavior.AllowGet);
    }
    

    【讨论】: