Ajax调用MVC控制器返回" NOT FOUND" [英] Ajax call to MVC Controller return "NOT FOUND"

查看:122
本文介绍了Ajax调用MVC控制器返回" NOT FOUND"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打电话与AJAX传递一些参数的MVC控制器动作。结果我在这个应用程序完成这有时并正常工作。结果我不知道为什么唯一的这ONE 不起作用。结果

I'm trying to call a MVC Controller action with AJAX passing some parameters.
I have done this sometimes in this application and it works fine.
I have no idea why only THIS ONE doesn't work.

 function excluirDetalhe(button, tab) {
     var index = $(button).closest('tr').index();
     var myTable = document.getElementById(tab);
     var id = myTable.rows[index].cells[0].innerHTML.trim();
     $('#' + tab + ' tr:eq(' + index + ')').remove();
         $.ajax({                
             traditional: true,
             url: "entidades/gravaCookie",
             type: 'post',
             data: { id: id, detalhe: "E" },
             error: function (jqXHR, textStatus, errorThrown) {
                 alert(errorThrown);
             }
     });
 }

这是我的控制器的方法:

This is my controller method:

public void gravaCookie(string id, string detalhe)
{
     string key = "een_id";
     if (detalhe.Equals("E"))
         key = "een_id";
     if (detalhe.Equals("C"))
         key = "eco_id";
     if (detalhe.Equals("B"))
         key = "eba_id";
     cookie.Values.Add(key, id);
}

只是一个提醒,我做正是我在其他Ajax做电话,但只有这一个特别是行不通的。结果没有人有任何想法?

Just a reminder that I'm doing exactly I did in other Ajax calls, but only this one in particular is not working.
Does anyone have any idea?

推荐答案

总是使用 Url.Action Url.RouteUrl HTML辅助方法建立的URL的操作方法。这将需要的不管你当前页面/路径正确地构建HTTP服务。

Always use the Url.Action or Url.RouteUrl html helper methods to build the url to the action methods. It will take care of correctly building the url regardless of your current page/path.

假设你的js code是剃刀视图中,可以直接使用 Url.Actio N法并分配给您的JS变量。

Assuming your js code is inside a razor view, you can directly use the Url.Action method and assign that to your js variable.

url: "@Url.Action("gravaCookie","entidades")",

它应该假设你有一个这样的操作方法

It should work assuming you have an action method like this

[HttpPost]
public ActionResult gravaCookie(string id,string detalhe)
{
  // to do : Return something
}

如果您的JavaScript是JavaScript文件,您可以用上面的helper方法建立在你的剃须刀查看URL(S),并保持在其外部js文件code可以访问变量一个单独的内部。 始终确保这样做时,使用JavaScript命名空间,以避免与全局JavaScript变量可能存在的问题

If your javascript is inside a seperate javascript file, you may build the url(s) in your razor view using the above helper methods and keep that in a variable which your external js file code can access. Always make sure to use javascript namespacing when doing so to avoid possible issues with global javascript variables.

@section Scripts
{
 <script>
    var myApp = myApp || {};
    myApp.Urls = myApp.Urls || {};
    myApp.Urls.baseUrl = '@Url.Content("~")';
    myApp.Urls.gravaCookieUrl = '@Url.Action("gravaCookie","entidades")';
 </script>
 <script src="~/Scripts/PageSpecificExternalJsFile.js"></script>

}

而在你的 PageSpecificExternalJsFile.js 文件,你可以像

var urlToGrava= myApp.Urls.gravaCookieUrl
// Or With the base url, you may safely add the remaining url route.
var urlToGrava2= myApp.Urls.baseUrl+"entidades/gravaCookie";
// Use urlToGrava now

这篇关于Ajax调用MVC控制器返回&QUOT; NOT FOUND&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆