【问题标题】:ASP.NET MVC Controller - 500ErrorASP.NET MVC 控制器 - 500 错误
【发布时间】:2013-05-24 16:49:15
【问题描述】:

为什么我的控制器函数没有被调用? 我总是得到 500error(在提琴手中)。我在 Visual Studio 或错误站点中没有收到任何错误。

控制器:

[POST("/test1")]  // attributerouting (works with GET methods)
public ActionResult test1(TreeViewItemModel aItem)
{
  ...
}

客户:

var tree = $("#demo2").jstree("get_json");
var c = JSON.stringify(tree);
$.ajax({
        type: "POST",
        url: "/test1",
        data: tree,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert(response);
        }
    });

【问题讨论】:

  • 您是在发帖,而不是在获取。我认为属性路由仅适用于 get。当您使用控制器名称作为 URL 的一部分时,您的操作会触发吗?
  • 为您的 ajax 调用添加错误处理程序并检查那里的响应
  • 不确定这是否是您的问题,但作为旁注,具有 route 属性的路由不应以正斜杠开头。然而,ajax url 仍应以 1 开头。

标签: asp.net ajax asp.net-mvc-4 routeattribute


【解决方案1】:

有时500 Internal Server Error 发生是因为它的控制器视图中的语法错误,例如{} 不匹配等。您检查test1.cshtml 的语法了吗?

【讨论】:

  • 我没有视图。我想将数据发布到服务器
  • 您是否尝试使用 Firebug 检查发布数据?
【解决方案2】:

问题是数据格式: 解决方案:

public ActionResult test1(IEnumerable<TreeViewItemModel> aItem)
{
}

客户:

var tree = $("#demo2").jstree("get_json");
var c = JSON.stringify(tree);
 $.ajax({
    type: "POST",
    url: "/test1",
    data: c,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        alert(response);
    }
});

【讨论】:

    猜你喜欢
    • 2016-08-06
    • 2014-11-10
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2020-12-11
    • 2011-03-01
    • 1970-01-01
    相关资源
    最近更新 更多