获取响应http angular
Retrieve get response http angular
我是一个初学者,我想在一些操作后检索url (http://myurl.com)的http get响应。
我测试了,但是没有结果:/
var app = angular.module('plunker', []);
app.factory('myService', function($http) {
return {
async: function() {
return $http.get('http://apibuilder-apidiscovery.kermit.rd.francetelecom.fr/infrastructures/16857');
}
};
});
app.controller('MainCtrl', function( myService,$scope) {
myService.async().then(function(d) { //2. so you can use .then()
$scope.data = d;
});
});
HTML <body>
<div ng-controller="MainCtrl">
Data: {{data}}<br>
</div>
</body>
数据赋值应为$scope.data = d.data;
app.controller('MainCtrl', function( myService,$scope) {
myService.async().then(function(d) { //2. so you can use .then()
$scope.data = d.data;
}, function(error){
console.log('Error occured');
});
});
注意还确保如果您试图呼叫外部域,应该启用
CORS
。此外,您可能需要在请求中传递授权头。
script.js
var app = angular.module('plunker', []);
app.factory('myService', function($http,$q) {
var myService = {
async : async,
};
return myService;
function async() {
var d = $q.defer();
$http({
method: 'GET',
url: 'http://apibuilder-apidiscovery.kermit.rd.francetelecom.fr/infrastructures/16857',
headers: {
"Content-Type": "text/plain",
// you can add headers here
}
}).then(function(reply) {
d.resolve(reply.data);
}, function(error) {
d.reject(error.data);
});
return d.promise;
}
});
app.controller('MainCtrl', function(myService, $scope) {
myService.async()
.then(function(result) {
$scope.data = result;
});
});
和索引文件
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="plunker" ng-controller="MainCtrl">
Data: {{data}}<br>
</body>
</html>
相关文章:
- Angular http.get没有'我不喜欢在开头和结尾加方括号的文本文件
- 使用angular$http和django-urlpatters来获得json文件
- 从angular$http获取promise数据
- Angular$http然后解析_.each中的多个调用
- Angular $http 服务不加载 json 文件
- 如何使用Angular$http.jsonp从S3加载JSON文件
- Angular$http.jsonp-服务器don'不接受JSON_CALLBACK
- Angular-$http范围问题
- Angular$http.post未到达服务器
- Angular$http返回数据,但不返回'不适用于范围
- Angular $http not invoking
- Angular http在Phone gap中返回错误,但在浏览器中成功
- 为什么可以't我取消了这个Angular HTTP请求
- Angular$http$资源等价于PDF GET请求
- 在服务中使用Angular$http在ng点击时触发
- 如何使用$q's构造函数语法与Angular$http's配置超时
- Angular http Get Always 返回 ng-click 的状态 0
- 循环遍历 Angular HTTP 请求中的对象,等到响应后再运行下一个循环
- Angular http 返回 $$state 对象
- PHP 和 Angular $http ajax 的问题