【问题标题】:Ajax calling wrong methodAjax调用错误的方法
【发布时间】:2012-10-22 14:01:43
【问题描述】:

我的 ajax 在 .net mvc 4 中调用了错误的方法,我不知道为什么。

我的 ajax:

function addItem(id, ammount) {
$.ajax({
    url: "/Shoppingcart/AddItem?id="+id+"&ammount="+ammount,
    type: "post",
    cache: false,
    success: function (result) {
        alert("SUCCESS!!!");
    },
    error: function (textStatus, errorThrown) {
        window.console.log(textStatus, errorThrown);
    }
});
}

我的 mvc 控制器:

public class ShoppingcartController : Controller
{
    //
    // GET: /Shoppingcart/

    public ActionResult Index()
    {
        // Method 1
    }

    [HttpPost]
    public ActionResult AddItem(int id = -1, int ammount = 0)
    {
        return Redirect("~/Home");
    }
}

我的第一个方法被 ajax 调用,这很奇怪,因为我调用 /Shoppingcart/AddItem 为什么会发生这种情况,我应该怎么做才能让它发挥作用?

解决方案: 问题不在于方法调用,而在于路由堆栈。显然,定义路线的顺序会影响它们的重要性。最具体的路线应该始终是要声明的第一条路线。

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Index",
            url: "{controller}/{id}",
            defaults: new { controller = "Home", action = "Index" },
            constraints: new { id = @"\d+" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "ControllerOnly",
            url: "{controller}",
            defaults: new { controller = "Home", action = "Index", id = 0 }
        );
    }

【问题讨论】:

  • 您的代码看起来不错,您确定调用了addItem 函数吗?请在 fiddler/dev-console/firebug 中检查您向服务器发送了哪些请求。
  • 根据提琴手的说法,正在调用 url localhost:1862/Shoppingcart/AddItem?id=18&ammount=1。除了上面我的成功消息被正确触发
  • 您是否有任何自定义路由,如果有,请发布您的路由配置?
  • 根据 jQuery 文档,您应该使用“POST”而不是“post”,但我真的怀疑它会改变什么... :-) 也许您应该使用“data”而不是查询字符串也。
  • nemesv 我将 routeconfig 添加到帖子中。 jovnas 我尝试使用数据,没有效果。

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


【解决方案1】:

问题在于您的路由,因为配置的路由按顺序匹配。

所以首先你应该把更具体的放在最后,然后把更通用的放在最后。

您必须首先使用url: "{controller}" 的最通用路由,它与url localhost:1862/Shoppingcart/AddItem?id=18&ammount=1 匹配,并使用action = "Index" 而不是操作AddItem

要修复它,请将您的路线顺序更改为:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Index",
    url: "{controller}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    name: "ControllerOnly",
    url: "{controller}",
    defaults: new { controller = "Home", action = "Index", id = 0 }
);

要使/Product/18 之类的网址正确路由,您需要使用id 上的约束而不是UrlParameter.Optional 来更改"Index" 路由,并且您需要将其放在"Default" 路由之前:

routes.MapRoute(
    name: "Index",
    url: "{controller}/{id}",
    defaults: new { controller = "Home", action = "Index" },
    constraints: new {id = @"\d+"}
);

【讨论】:

  • 这听起来合乎逻辑,但是当我应用上述“解决方案”时,我的 localhost:1862/Product/18 不再工作了
  • 太棒了!它现在(有点)有效!至少该功能被调用,但是(请参阅更新的问题)我的重定向被忽略了。怎么来的? (根据我的调试器触发了该行,但未采取任何操作)
  • 如果路由正常,那么您的问题将得到解答。为什么重定向被“忽略”是一个不同的问题,但我现在可以回答:重定向在 AJAX 请求中不起作用:请参阅此 SO 问题以获取解决方案:stackoverflow.com/questions/199099/…
猜你喜欢
  • 2013-07-18
  • 2019-04-01
  • 1970-01-01
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
  • 1970-01-01
相关资源
最近更新 更多