我尝试创建一个函数,该函数在Javascript中生成HTTP请求并获取此请求的结果。不幸的是,我绝对不知道如何回到这个结果在其他功能..获取Angular JS中HTTP请求的值
在这里找到我的函数的两个(两者都应该做同样的事情):
$scope.getInfo = function() {
return $http({
method: 'GET',
url: 'https://api.net'
}).then(function (response) {
return response.data;
});
};
而另外一个:
$scope.getInfo = function() {
var defer = $q.defer();
$http.get('https://api.net').then(function(response) {
defer.resolve(response.data);
}, function(response) {
defer.reject(response);
});
return defer.promise;
};
我已经找到了很多篇关于发出请求的方式,但不能取回它的值(函数在其他一个简单的电话只显示“目标对象”,我没”找到正确显示的解决方案)。
$scope.test = function() {
var myValue = $scope.getInfo();
alert(myValue); /* show [Object object] */
};
你能帮我吗?