重新加载页面提供了AngularJS HTML5模式的错误GET请求

前端之家收集整理的这篇文章主要介绍了重新加载页面提供了AngularJS HTML5模式的错误GET请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想为我的应用启用HTML5模式。我已经为配置放了以下代码,如图所示 here
  1. return app.config(['$routeProvider','$locationProvider',function($routeProvider,$locationProvider) {
  2.  
  3. $locationProvider.html5Mode(true);
  4. $locationProvider.hashPrefix = '!';
  5.  
  6. $routeProvider.when('/',{
  7. templateUrl: '/views/index.html',controller: 'indexCtrl'
  8. });
  9. $routeProvider.when('/about',{
  10. templateUrl: '/views/about.html',controller: 'AboutCtrl'
  11. });

如你所见,我使用$ locationProvider.html5模式,我改变了所有的链接在ng-ref排除/#/。

问题

目前,我可以去localhost:9000 /和看到索引页,并导航到其他页像localhost:9000 / about。

但是,当我刷新本地主机:9000 / about页时,会出现问题。我得到以下输出:不能GET /约

如果我看网络电话:

  1. Request URL:localhost:9000/about
  2. Request Method:GET

如果我先去localhost:9000 /然后点击一个按钮导航到/ about我得到:

  1. Request URL:http://localhost:9000/views/about.html

这将完美地呈现页面

如何在刷新时启用angular以获取正确的页面

先谢谢你。

angular docs

Server side
Using this mode requires URL rewriting on server side,basically you have to rewrite all your links to entry point of your application (e.g. index.html)

原因是当您第一次访问该网页(/ about)时,刷新后,浏览器无法知道这不是一个真正的URL,所以它继续加载它。然而,如果你已经加载了根页面和所有的JavaScript代码,那么当你导航到/ about Angular可以在浏览器尝试打开服务器并处理它之前

猜你在找的Angularjs相关文章