gpt4 book ai didi

backbone.js - Backbone pushState 和错误 404

转载 作者:行者123 更新时间:2023-12-04 06:05:03 25 4
gpt4 key购买 nike

我正在尝试实现 { pushState : true }但它仅适用于基本路线,不适用于其他继续给我错误 404 的路线。

在 Chrome 中,如果我访问:

http://example.app/ - 确定显示控制台消息

http://example.app/show - 返回错误 404

我的路线是

    var AppRouter = Backbone.Router.extend({

routes: {
'': 'index',
'show': 'show'
},

index: function() {
console.log('This is the index page');
},
show: function() {
console.log('This is the show page');
}

});

new AppRouter;
Backbone.history.start({pushState: true});

我的.htaccess
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
</ifModule>

我缺少什么或我做错了什么?

最佳答案

请记住,Backbone 是一个客户端框架——如果您使用基于路径的 URL 进行路由(推送状态),您仍然需要确保服务器为这些 URL 返回正确的标记。这是在Backbone docs中总结的因此:

Note that using real URLs requires your web server to be able to correctly render those pages, so back-end changes are required as well. For example, if you have a route of /documents/100, your web server must be able to serve that page, if the browser visits that URL directly. For full search-engine crawlability, it's best to have the server generate the complete HTML for the page ... but if it's a web application, just rendering the same content you would have for the root URL, and filling in the rest with Backbone Views and JavaScript works fine.



换句话说,如果您的服务器不理解 example.app/show,Backbone 将无法帮助您。 -- 您必须使用 URL 重写和/或您选择的服务器端语言来修复服务器。

关于backbone.js - Backbone pushState 和错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15349648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com