Angularjs routing throws 404 without #

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-05 04:33:12

问题


I am creating a website with AngularJS and I am using the ngRoute module to handle page requests.

In my app.config function I have setup the routing as follows:

app.config(function($routeProvider, $locationProvider, $httpProvider){
    $locationProvider.html5Mode(true);
    $routeProvider.when('/', {
        templateUrl: 'pages/index.html',
        controller: 'mainController'
    }).when('/testpage', {
        templateUrl: 'pages/test.html',
        controller: 'testController'
    }).otherwise({
        redirectTo: '/'
    });
});

When I try to access the test page, on my MAMP local server, by adding a # before the page name like this: http://dev.mysite.com:8888/#testpage everything works fine.

However, if I don't add the # before the page name, like this: http://dev.mysite.com:8888/testpage I get a 404 error.

I have added <base href="/"> to the <head> of my index.html page that contains the <div ng-view> tag.

I would very much appreciate it if someone could explain what I am doing wrong.


回答1:


Angular looks for the # symbol to handle routing. Everything after the # symbol is interpreted by the routing function. No # symbol, no angular route. If you can run in Html5 mode, then you can turn this off, but to work with older browsers, it is needed.



来源:https://stackoverflow.com/questions/29088017/angularjs-routing-throws-404-without

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