我正在尝试使用 $resource
从 Angular 向指定的路由发出 get/query 请求,该路由将 ping API,然后我应该获取一个包含来自 API 的结果的对象。
这是一个搜索功能。查看本期的流程:
Angular 服务:
angular.module('MyApp')
.factory('Search', function($resource) {
return $resource('/api/shows/:_search');
});
Ctrl:
$scope.$watch('searchStr', function (tmpStr)
{
if (!tmpStr || tmpStr.length == 0)
return 0;
// if searchStr is still the same..
// go ahead and retrieve the data
if (tmpStr === $scope.searchStr) {
Search.query({'search': $scope.searchStr})
.$promise.then(function(data) {
$scope.responseData = data;
})
}
});
查看:
<input type="text" data-ng-model="searchStr">
<textarea> {{responseData}} </textarea>
Node.js:
app.get('api/shows/:search', function(req, res, next) {
console.log(req, res);
request.get('http://thetvdb.com/api/GetSeries.php?seriesname=' + req.params.search, function (error, response, body) {
console.log(error, response, body);
});
});
这就是我需要的,我需要对 'api/shows/:search'
执行 get 请求并执行某些操作才能从 http://thetvdb 获取结果.com/api/GetSeries.php?seriesname=' + req.params.search
但我仍然在努力解决它应该如何完成。 search
参数是来自 Angular 的字符串,以便转到 thetvdb
并返回我需要的内容。
以下是如果您发送带有单词“all”的字符串参数时应返回的示例:http://www.thetvdb.com/api/GetSeries.php?seriesname=all&language=en
有什么建议吗?
最佳答案
至少你的nodejs路由必须返回响应中的字符串:
app.get('api/shows/:search', function(req, res, next) {
request.get('http://thetvdb.com/api/GetSeries.php?seriesname=' + req.params.search, function (error, response, body) {
console.log(error, response, body);
res.end(body);
});
});
关于javascript - 如何对路由发出 get 请求并从 API 检索信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37350341/