c# - .NET MVC 的 jQuery getJSON 不工作

标签 c# jquery asp.net asp.net-mvc asp.net-mvc-5

当页面加载到我的 .NET MVC 应用程序中时,我已经设置了一个 getJSON 调用,如下所示:

$(document).ready(function(){
   $.getJSON("/Administrator/GetAllUsers", function (res) {

            // getting internal server error here...
        });

});

Action 看起来像这样:

  [HttpGet]
    [ActionName("GetAllUsers")]
    public string GetAllUsers()
    {
        return new JavaScriptSerializer().Serialize(ctx.zsp_select_allusers().ToList());
    }

我收到这个错误:

500 (Internal Server Error)

我在这里做错了什么???

最佳答案

在 MVC 中,仅当您出于某些安全目的向它发出 post 请求时才返回 json 结果,直到并且除非您明确指定 JsonRequestBehavior.AllowGet,否则也会更改您的返回值类型

[HttpGet]
    [ActionName("GetAllUsers")]
    public JsonResult GetAllUsers()
    {
        return Json(ctx.zsp_select_allusers(), JsonRequestBehavior.AllowGet);
    }

关于c# - .NET MVC 的 jQuery getJSON 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43229760/

相关文章:

javascript - 使用 Firebug 调试触发了哪个函数

asp.net - asp.net 复选框的 CSS 选择器

javascript - 如何使用 Javascript 或 jQuery 在选项属性中添加 "selected"?

jquery - Bootstrap Modal 是透明的 - 不透明度不起作用

c# - 调试实时 ASP.net 网站

asp.net - HttpContext.Current.Response 和 Page.Response 有什么区别?

c# - 具有身份验证的 MVC 应用程序中的 Web API 基本验证

c# 将类转换为查询字符串

c# - Windows 手机 : Application Exits after handling Exception from DialogForm

c# - 如何关闭当前浏览器窗口并重定向到同一浏览器先前打开的窗口