[英]AngularJS $http.get not working with external domain json data
I am trying to pull data from external domain using angularjs. 我正在尝试使用angularjs从外部域中提取数据。 I dont see any issue in the console.
我在控制台中看不到任何问题。 I tried to my best and fiddled here .
我竭尽全力在这里摆弄 。 Any help or pointer would be great.
任何帮助或指针将是巨大的。
Vie Code 竞争代码
<div ng-controller="myCtrl">{{response}}
<button ng-click="getData()">GET Request</button>
</div>
Controller code 控制器代码
angular.module('myApp', [])
.controller('myCtrl', function ($scope, $http) {
$scope.getData = function () {
$http.get("http://api.openweathermap.org/data/2.5/weather?zip=98007,us").success(function (response) {
$scope.myData = response;
console.log(response)
});
};
});
Your code is fine but you are missing a few things in your markup. 您的代码很好,但是您在标记中遗漏了一些东西。 You are missing
ng-app
and you should be doing {{myData}}
and not {{response}}
(because myData
is bound to your scope and assigned to response
, there is not variable named response
bound to your scope). 您缺少
ng-app
,应该进行{{myData}}
而不是{{response}}
(因为myData
已绑定到您的作用域并分配给response
,所以没有变量名为response
绑定到您的作用域)。 See plunker 见朋克
<body ng-app="myApp">
<div ng-controller="myCtrl">{{myData}}
<button ng-click="getData()">GET Request</button>
</div>
</body>
Using the browser dev tools what does the the network tab show ? 使用浏览器开发工具,“网络”选项卡显示什么? Was the request successful, was the request even made ?
请求成功了吗?
Just add ng-app
in body or html 只需在正文或html中添加
ng-app
By looking at your jsFiddle is clear that your issues is that you are not adding the app name in your html. 通过查看您的jsFiddle,可以清楚地看到您的问题是您没有在html中添加应用程序名称。
Here is your update jsFiddle : http://jsfiddle.net/kn4yaoco/3/ 这是您的更新jsFiddle: http : //jsfiddle.net/kn4yaoco/3/
you can see in console that your request is giving response properly. 您可以在控制台中看到您的请求正在正确响应。
You should .then
with $http, so something like: 您应该使用$ http
.then
,然后这样:
$http.get("http://api.openweathermap.org/data/2.5/weather?zip=98007,us")
.then(function(response) {
console.log(response);
}):
Best 最好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.