Backbone Routes Not Being Called

自作多情 提交于 2019-12-05 01:41:53

As discussed in the comments, the problem is that, when using push-state style URLs, the server doesn't recognize the Backbone route URLs.

For illustration, say your application's root is at server/app/index.html, and you're trying to use a URL that Backbone routes to /report/print. With URL fragment routing, this is fine:

http://server/app/index.html#report/print

The server ignores the part after # and returns index.html; then on load Backbone routes to report/print.

But if you're using push-state routing, then the URL looks like this:

http://server/app/index.html/report/print

And the server throws a 404 error because it doesn't recognize anything at that path, so Backbone is never even loaded.


The solution is to either:

  1. As the Backbone.js docs note, modify server code, so that the server renders the correct content for each Backbone route, or
  2. (which I think is easier) put a URL rewrite in place on the web server (IIS, Apache), so that it will return index.html for any request that is a Backbone route like index.html/report/print, index.html/report/add, etc.

In IIS, for example, you'd put the following in the web.config under your application root:

<rewriteMaps>
    <rewriteMap name="StaticRewrites">
        <add key="index.html/report/print" value="index.html" />
        <add key="index.html/report/add" value="index.html" />
        <!-- etc -->
    </rewriteMap>
</rewriteMaps>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!