Backbone pushState and error 404

浪子不回头ぞ 提交于 2019-12-05 05:15:08

Remember that Backbone is a client-side framework -- if you're using path-based URLs for routing (push state), you still need to ensure that the server returns the correct markup for those URLs. This is summed up in the Backbone docs thusly:

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.

In other words, Backbone can't help you if your server doesn't understand example.app/show -- you have to fix the server, using a URL rewrite, and/or your server-side language of choice.

Shobhit Sharma

You need to create an initialize function for that case.

I hooked up something on Boilerplate router, just include this before initializing router in end of your script.

var initialize = function() {

    var app_router = new AppRouter;

    Backbone.history.start({ pushState: false });

    $(document).on('click', 'a:not([data-bypass])', function(e){

        var href = $(this).prop('href');
        var root = location.protocol + '//' + location.host + '/';

        if (root === href.slice(0, root.length)){
            e.preventDefault();
            Backbone.history.navigate(href.slice(root.length), true);
        }
    });

};

I think you may just be missing the "#" in your url. I was following some tutorials now and I just realised that how they stop the request from going to the server.

So instead of
http://example.app/show
have
http://example.app/#/show

and backbone should be able to catch it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!