[英]Cannot figure out why the link is not being found in angular
I am sending a request 我正在发送请求
$http.get('/loadpage?category='+$scope.selectedCategory+'&page='+$scope.selectedPage).then(function(response){
}).catch(function(){
console.log("Error caught!");
});
On my router.js file, 在我的router.js文件中,
I have 我有
router.get('/loadpage?category=xx&page=yy', function(req, res) {
res.send("Is this working");
});
I am not sure why my server is showing the following error. 我不确定为什么我的服务器显示以下错误。
angular.js:12587 GET http://localhost:3000/loadpage?category=1&page=1 404 (Not Found) angular.js:12587 GET http:// localhost:3000 / loadpage?category = 1&page = 1 404(未找到)
Because I tried sending a request to another url 因为我尝试将请求发送到另一个网址
router.get('/runthis/category=:xx',function(req, res){
var p = req.params.xx;
res.send(p);
});
by 通过
function runthis (){
$http.get('runthis/category=smackthat').then(function(response){
console.log(response.data);
}).catch(function(){
console.log("Error caught!");
});
}
And it works perfectly. 而且效果很好。
Can anyone help. 谁能帮忙。 Thanks 谢谢
您正在对查询字符串(URL的?foo=bar
部分)进行操作,以表示查询字符串已解析为req.query,例如req.query.foo === 'bar'
因此路由应为/loadpage
和您应该从req.query获取数据
Why don't you make your url a bit simpler such as : 为什么不将您的网址简化一些:
router.get('/loadpage/:category/:page', function(req, res) {
res.send("Is this working");
});
Makes your url parameters more semantic. 使您的url参数更具语义。 and call it as : 并将其称为:
$http.get('/loadpage/'+$scope.selectedCategory+'/'+$scope.selectedPage).then(function(response){
}).catch(function(){
console.log("Error caught!");
});
Also, when you defined your url without :
in router.get('/loadpage?category=xx&page=yy'...
The router is looking for an exact match for the called url, in your case, you are passing variables and unless those 2 have xx
and yy
in them respectively, it's working as expected and throwing 404 另外,当您在router.get('/loadpage?category=xx&page=yy'...
定义不带:
的url时,路由器正在寻找被调用url的完全匹配项,在这种情况下,您正在传递变量,除非那两个分别有xx
和yy
,它按预期方式工作并抛出404
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.