使用AngularJS JSONP加载外部资源时出错

时间:2017-12-04 03:30:29

标签: angularjs json ajax jsonp

我真的很困惑如何使用$ http.jsonp加载外部资源。这是我的代码:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<ul>
{{myData}}
</ul>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
var url = "https://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero";
  $http.jsonp(url).then(function (response) {
      $scope.myData = response.data;
  });
});
</script>

</body>
</html>

我得到这样的错误:

  

错误:[$ sce:insecurl]

我的代码有什么问题?谢谢。

1 个答案:

答案 0 :(得分:0)

将角度版本从1.6.4更新为1.2.6

,工作正常

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
var url = "https://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero";
  $http.jsonp(url).then(function (response) {
  console.log(response)
      $scope.myData = response.data;
      
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl"></div>