【发布时间】: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.html或www.mysite.com/pages/about.html? -
@JasonP 我不会在任何地方写相对链接。它的 sammy 路由库正在添加当前 url + 表单的操作(“#/login”),因此它变为“/Account/Login#login”。即使我无法弄清楚这一点,因为我希望 sammy routing 会寻找“#/login”路径。
-
@Yoda 你绝对使用相对路径。研究 URL 片段的工作原理。
标签: javascript jquery ruby-on-rails sammy.js