【问题标题】:Not able to match existing route path and getting 404 Not Found无法匹配现有路由路径并获得 404 Not Found
【发布时间】:2014-04-24 14:20:07
【问题描述】:

我有以下 html 标记,其中操作是 sammyjs 路由代码中定义的路由路径之一:

<form id="loginform" action="#/login" method="post" class="form-horizontal">

<!-- input & hidden elements -->

<!-- submit button -->

</form>

如果上面的视图是通过 "localhost:xxx/Account/Login" 之类的 url 加载的,当我提交上面的表单时,sammyjs 会记录它无法找到路由的错误:

[Thu Apr 24 2014 13:52:39 GMT+0100 (GMT Daylight Time)] #shell 404 Not Found get /Account/Login#/login  
Error {message: "404 Not Found get /Account/Login#/login ", stack: "Error↵    at Object.Sammy.Application.$.extend.err…/localhost:36141/Scripts/Vendor/require.js:132:23"}
message: "404 Not Found get /Account/Login#/login "
stack: "Error↵    at Object.Sammy.Application.$.extend.error (http://localhost:36141/Scripts/Vendor/sammy-0.7.4.js:1346:47)↵    at Object.Sammy.Application.$.extend.notFound (http://localhost:36141/Scripts/Vendor/sammy-0.7.4.js:1335:22)↵    at Object.Sammy.Application.$.extend.runRoute (http://localhost:36141/Scripts/Vendor/sammy-0.7.4.js:1179:21)↵    at Object.Sammy.Application.$.extend._checkLocation (http://localhost:36141/Scripts/Vendor/sammy-0.7.4.js:1365:25)↵    at Object.Sammy.Application.$.extend.run (http://localhost:36141/Scripts/Vendor/sammy-0.7.4.js:971:12)↵    at http://localhost:36141/Scripts/App/common.js:36:19↵    at Object.context.execCb (http://localhost:36141/Scripts/Vendor/require.js:1650:33)↵    at Object.Module.check (http://localhost:36141/Scripts/Vendor/require.js:866:51)↵    at Object.<anonymous> (http://localhost:36141/Scripts/Vendor/require.js:1113:34)↵    at http://localhost:36141/Scripts/Vendor/require.js:132:23"
__proto__: d

我无法理解为什么 sammy 会在我的表单操作设置为 "#/login" 时查找 "/Account/Login#/login" >.

我还尝试将我的脚本更改为上面错误中提到的路由路径,例如从 "#/login""/Account/Login#/login",但我又得到了与上面链接中提到的相同的错误。

this.post('#/login', function (context) {
//process
});

其他链接或表单标签也是如此。我想使用的哈希标记会添加到 URL 中的任何内容后缀,例如 "/Account/Login#{/any_hash_tag}"。 我该怎么做才能解决这个问题?

但是,如果视图是通过像 "localhost:xxx/" 这样没有其他路由参数的 url 加载的,则一切正常。

很高兴知道我是否做错了什么。

干杯

【问题讨论】:

  • 您正在使用相对链接...如果您正在查看www.mysite.com/pages/index.html 并且有一个链接的href 为about.html,您希望它带您去哪里? www.mysite.com/about.htmlwww.mysite.com/pages/about.html?
  • @JasonP 我不会在任何地方写相对链接。它的 sammy 路由库正在添加当前 url + 表单的操作(“#/login”),因此它变为“/Account/Login#login”。即使我无法弄清楚这一点,因为我希望 sammy routing 会寻找“#/login”路径。
  • @Yoda 你绝对使用相对路径。研究 URL 片段的工作原理。

标签: javascript jquery ruby-on-rails sammy.js


【解决方案1】:

这就是URL Fragments 的工作原理。

Sammy 期望路由以域名开头,后跟斜杠。
链接到#[anything] 的锚标记会将该 URL 片段附加到当前视图的末尾。

你可以通过几种方式解决这个问题:

  1. 完全使用 Sammy,或者根本不使用它。我不确定你为什么会有像/Account/Login#/login 这样的路由,但它看起来很乱,而且看起来有两个路由器。这不好玩。像#/account/login 一样使用 Sammy。
  2. 使您的链接操作执行/#/login。这会将基本路由重置为 Web 根目录,然后将片段附加到末尾。

【讨论】:

  • 我之前关注 #2 并使用加载程序查看,但是当我将默认 url 更改为“/Account/Login”时,我开始收到此路由未找到错误。此外,查看sammyjs.org/docs/routes 的路径部分,sammy 也可以使用相对 url。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-07
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
  • 2020-02-04
  • 2019-01-31
  • 1970-01-01
相关资源
最近更新 更多