gpt4 book ai didi

javascript - AngularJS $http.get 无法使用外部域 json 数据

转载 作者:行者123 更新时间:2023-11-28 15:19:08 25 4
gpt4 key购买 nike

我正在尝试使用 angularjs 从外部域提取数据。我在控制台中没有看到任何问题。我尽力了fiddled here 。任何帮助或指示都会很棒。

查看代码

<div ng-controller="myCtrl">{{response}}
<button ng-click="getData()">GET Request</button>
</div>

Controller 代码

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)
});
};
});

最佳答案

您的代码很好,但您的标记中缺少一些内容。您缺少 ng-app 并且您应该执行 {{myData}} 而不是 {{response}} (因为 myData 绑定(bind)到您的作用域并分配给 response,没有名为 response 的变量绑定(bind)到您的作用域)。请参阅plunker

  <body ng-app="myApp">
<div ng-controller="myCtrl">{{myData}}
<button ng-click="getData()">GET Request</button>
</div>
</body>

关于javascript - AngularJS $http.get 无法使用外部域 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32282660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com