0

在我的 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' 如果出现问题,我应该在调试时得到这个,但我没有。为什么我会收到此错误?谢谢

4

1 回答 1

0

如果 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
    }
});
于 2012-06-22T21:58:54.693 回答