无法弄清楚为什么没有找到角度链接

时间:2018-04-27 13:50:06

标签: angularjs node.js

我正在发送请求

$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获取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!");
    });
}

它完美无缺。

任何人都可以提供帮助。感谢

2 个答案:

答案 0 :(得分:1)

您正在查询字符串(网址的Eloquent\Builder部分)上运行,查询字符串会被解析为req.query,例如?foo=bar,因此路由应该只是{{1你应该从req.query

获取数据

答案 1 :(得分:0)

为什么不让你的网址更简单,例如:

router.get('/loadpage/:category/:page', function(req, res) {
   res.send("Is this working");
});

使您的网址参数更具语义性。并将其命名为:

$http.get('/loadpage/'+$scope.selectedCategory+'/'+$scope.selectedPage).then(function(response){

}).catch(function(){
    console.log("Error caught!");
});

另外,当你在:中定义了没有router.get('/loadpage?category=xx&page=yy'...的网址时,路由器正在寻找被叫网址的完全匹配,在你的情况下,你传递的是变量,除非那些有xx yy分别为1}}和{{1}},它按预期工作并抛出404