【问题标题】:Getting 404s when calling Actions in MVC3 with jQuery使用 jQuery 在 MVC3 中调用操作时出现 404
【发布时间】:2012-06-25 05:56:26
【问题描述】:

在我的 MVC3 应用程序中,我使用 $.ajax 调用 JsonResult 类型的方法来获取要显示的数据:

 function GetData(e) {
    var ordersId = e.row.cells[0].innerHTML; //this is fine
    $.ajax({
         type: "POST",
         url: "/Documents/GetDocumentData",
         contentType: "application/json; charset=utf-8",
         data: "{'id': '"+ordersId +"'}",
         dataType: "json",
         success: function (result) {
        //load window
         },
         error: function (result) {
            if (!result.success)
        //show error
         }
     });

这是我的行动:

   [HttpPost] 
        public JsonResult GetDocumentData(string id)
    {
           //my code
           return Json(new { success = true});

        }

当我在我的开发机器上调试时,它工作正常。我将它部署到我的测试网络服务器上,我得到'404 page not found dev/testwebsite/Documents/GetDocumentData' 如果出现问题,我应该在调试时得到这个,但我没有。为什么我会收到此错误?谢谢

【问题讨论】:

  • 您可以从哪个网址访问Action
  • 我不明白你的问题......
  • 为什么在测试应用程序时视图中显示的 url?

标签: c# javascript asp.net-mvc-3 jquery


【解决方案1】:

如果 dev/testwebsite/Documents/GetDocumentData 是服务器上的 URL,则您在 javascript 中的 URL 是错误的。

您应该使用 @Url.Action() 在 cshtml 页面中自动形成这些 URL。

例子:

@Url.Action("actionName","controllerName"  )

因此,对于您的具体情况,应该是:

@Url.Action( "GetDocumentData", "Documents" )

在 javascript 中,它看起来像这样:

function GetData(e) {
    var ordersId = e.row.cells[0].innerHTML; //this is fine
    $.ajax({
        type: "POST",
        url: '@Url.Action("GetDocumentData","Documents")',
        contentType: "application/json; charset=utf-8",
        data: "{'id': '"+ordersId +"'}",
        dataType: "json",
        success: function (result) {
        //load window
    },
    error: function (result) {
        if (!result.success)
        //show error
    }
});

【讨论】:

  • 你的意思是这样的:url:@Html.ActionUrl("/Documents/GetDocumentData")。在我的 $.ajax 调用中?
  • 这不是您发布的内容...并且您发布的网址与javascript中的网址不一致。
  • 我该如何使用 Html.ActionUrl 是我的问题?
  • 是在 CSHTML 文件中还是在单独的 .js 文件中?
  • 好的。然后 Visual Studio 应该向您展示该函数的几个重载。但是,一般来说,您将其称为稍后将出现在答案中的示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
相关资源
最近更新 更多