admin管理员组文章数量:1325380
I have a search function within my Angular app that when executed hits my API to grab results then redirects the user using $location.url
. Everything works great...until I try to reload the results page. When I do so I get a Cannot GET /search/[search-term]
message.
app.config.js:
'use strict';
angular.module('gameApp')
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/player/:playerId', {
templateUrl: 'player.html'
})
.when('/search/:searchTerm', {
templateUrl: '/app/gmSearchResults/gmSearchResults.html'
});
$locationProvider.html5Mode({
enabled: true,
requireBase: false,
rewriteLinks: false
});
});
relevant navbar.controller.js:
vm.getSearchResults = function(searchTerm) {
searchService.resource.getResults({
query: searchTerm
}).$promise.then(function(results) {
if (results.playerId) {
$location.url('/customer/' + results.playerId);
} else {
saveLatest(results);
$location.url('/search/' + searchTerm);
}
});
};
I have a search function within my Angular app that when executed hits my API to grab results then redirects the user using $location.url
. Everything works great...until I try to reload the results page. When I do so I get a Cannot GET /search/[search-term]
message.
app.config.js:
'use strict';
angular.module('gameApp')
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/player/:playerId', {
templateUrl: 'player.html'
})
.when('/search/:searchTerm', {
templateUrl: '/app/gmSearchResults/gmSearchResults.html'
});
$locationProvider.html5Mode({
enabled: true,
requireBase: false,
rewriteLinks: false
});
});
relevant navbar.controller.js:
vm.getSearchResults = function(searchTerm) {
searchService.resource.getResults({
query: searchTerm
}).$promise.then(function(results) {
if (results.playerId) {
$location.url('/customer/' + results.playerId);
} else {
saveLatest(results);
$location.url('/search/' + searchTerm);
}
});
};
Share
Improve this question
asked Jul 20, 2015 at 16:17
MattDionisMattDionis
3,61610 gold badges55 silver badges110 bronze badges
1
- Are you sure that server response index.html when you try to load specific page? Try to turn off html5 mode and check it. – user1050438 Commented Jul 20, 2015 at 16:31
2 Answers
Reset to default 7Setup your express app:
app.route('/*').get(function(req, res) {
return res.sendFile(path.join(config.root, 'index.html'));
});
Just a small edit to Georgii Kats answer. Try using:
app.get('/*', function (req, res, next) {
if (/.js|.html|.css|templates|js|scripts/.test(req.path) || req.xhr) {
return next({ status: 404, message: 'Not Found' });
} else {
return res.render('index');
}
});
本文标签: javascriptAngular 39Cannot GET39 route on page refreshStack Overflow
版权声明:本文标题:javascript - Angular 'Cannot GET' route on page refresh - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742192234a2430424.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论