所以我正在向我的快速搜索路由器发出post请求,在那里我使用节点获取模块调用远程api:
var fetch = require('node-fetch');
router.route('/search')
//Performs Job Search
.post(function(req, res){
var area = req.body.area;
fetch('https://api.indeed.com/ads/apisearch?publisher=*********&l=' + area)
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
});
})
我在客户端使用angular 1来调用路由器并解析返回的json:
$scope.search = function(){
$http.post('/api/search', $scope.newSearch).success(function(data){
if(data.state == 'success'){
//perform JSON parsing here
}
else{
$scope.error_message = data.message;
}
});
}
我刚开始使用MEAN堆栈,对承诺是如何工作的只有一个模糊的概念。所以我的问题是,我的角搜索函数没有得到JSON字符串,我想从我的远程API调用返回。而是数据参数被设置为我的页面html。最终,我在.then()子句中设置的断点被命中,并返回json。所以我的问题是如何使用angular来获取JSON值,当它们最终返回????
你能试试这样吗?
router.route('/search')
//Performs Job Search
.post(function(req, res){
var area = req.body.area;
fetch('https://api.indeed.com/ads/apisearch?publisher=*********&l=' + area)
.then(function(result) {
res.json(result);
//or possibly res.send(result), depending on what indeed responds with
})
})
原来我忘记了我有一个中间件,如果用户在执行搜索时没有经过身份验证,他们将被重定向到登录页面。所以我得到了一堆html返回这个登录页面,而不是我的json。仍然让我困惑的是,如果我在到达这个函数之前被重定向,为什么我在搜索函数中的断点会被击中。