[英]Cannot figure out why the link is not being found in angular
我正在發送請求
$http.get('/loadpage?category='+$scope.selectedCategory+'&page='+$scope.selectedPage).then(function(response){
}).catch(function(){
console.log("Error caught!");
});
在我的router.js文件中,
我有
router.get('/loadpage?category=xx&page=yy', function(req, res) {
res.send("Is this working");
});
我不確定為什么我的服務器顯示以下錯誤。
angular.js:12587 GET http:// localhost:3000 / loadpage?category = 1&page = 1 404(未找到)
因為我嘗試將請求發送到另一個網址
router.get('/runthis/category=:xx',function(req, res){
var p = req.params.xx;
res.send(p);
});
通過
function runthis (){
$http.get('runthis/category=smackthat').then(function(response){
console.log(response.data);
}).catch(function(){
console.log("Error caught!");
});
}
而且效果很好。
誰能幫忙。 謝謝
您正在對查詢字符串(URL的?foo=bar
部分)進行操作,以表示查詢字符串已解析為req.query,例如req.query.foo === 'bar'
因此路由應為/loadpage
和您應該從req.query獲取數據
為什么不將您的網址簡化一些:
router.get('/loadpage/:category/:page', function(req, res) {
res.send("Is this working");
});
使您的url參數更具語義。 並將其稱為:
$http.get('/loadpage/'+$scope.selectedCategory+'/'+$scope.selectedPage).then(function(response){
}).catch(function(){
console.log("Error caught!");
});
另外,當您在router.get('/loadpage?category=xx&page=yy'...
定義不帶:
的url時,路由器正在尋找被調用url的完全匹配項,在這種情況下,您正在傳遞變量,除非那兩個分別有xx
和yy
,它按預期方式工作並拋出404
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.