[英]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.