使用Html5Mode刷新页面时出现意外的令牌

时间:2014-09-11 12:51:52

标签: angularjs .htaccess angular-ui-router

我在AngularJS应用中使用Html5Mode(true)。当我浏览我的网站时,这很好用:

http://www.example.com

然后转换为:

http://www.example.com/home/index

我的页面很好。

但是,当我按F5时,我的控制台窗口出现了很多错误:

Resource interpreted as Script but transferred with MIME type text/html: "http://www.example.com/home/js/jquery/jquery.min.js". index:22
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://www.example.com/home/js/slider/angular.rangeSlider.css". index:16
Uncaught SyntaxError: Unexpected token < jquery.min.js:1
Resource interpreted as Script but transferred with MIME type text/html: "http://www.example.com/home/js/angular/angular-resource.min.js". index:25
Resource interpreted as Script but transferred with MIME type text/html: "http://www.example.com/home/js/angular/angular.min.js". index:24
Uncaught SyntaxError: Unexpected token < angular.min.js:1
Uncaught SyntaxError: Unexpected token < angular-resource.min.js:1
Resource interpreted as Script but transferred with MIME type text/html: "http://www.example.com/home/js/angular/angular-cookies.min.js". index:26
Uncaught SyntaxError: Unexpected token < 

我已经配置了.htaccess这样的文件:

RewriteEngine on

# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^ - [L]

# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^(.*) /index.html [L]

但是当我刷新页面时,这不起作用。然后我得到错误。有什么我想念的吗?


我正在使用ui-router。不确定这是否有所不同。我的路线看起来像这样:

$locationProvider.html5Mode(true);

$urlRouterProvider.otherwise('/home/index');
$stateProvider
    .state('home', {
        abstract: true,
        url: '/home',
        templateUrl: '/tpl/home.html'
    })
    .state('home.index', {
        url: '/index',
        templateUrl: '/tpl/home_index.html'
    })

任何人都知道为什么刷新页面会导致我上面发布的错误?

1 个答案:

答案 0 :(得分:0)

您应该只重写所需的路由器URL,例如:

/ home - index.html

/ product / 1234 - index.html

不要重写对index.html的所有请求。因为大多数网址不应该重写。