所以我正在向我的Express搜索路由器发送一个帖子请求,我正在使用node-fetch模块来调用远程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);
});
})
我在客户端使用角度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堆栈,只是对promises如何工作有一个模糊的概念。所以我的问题是我的角度搜索功能没有获得我想从远程API调用返回的JSON字符串。而是数据参数设置为我的页面html。最终,我在.then()子句中设置的断点被命中,我的json被返回。所以我的问题是如何在最终返回时使用Anguular获取JSON值????
答案 0 :(得分:0)
你能尝试这样的事吗?
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
})
})
答案 1 :(得分:0)
事实证明我忘记了我有中间件,如果用户在执行搜索时未经过身份验证,则会将其重定向到登录页面。所以我得到了一堆返回此登录页面的html,而不是我的json。让我感到困惑的是,为什么我的搜索功能中的断点如果在我到达此功能之前被重定向,就会被击中。