Search code examples
angularjsjsonajaxjsonp

Error while loading external resources using AngularJS JSONP


Im really confused on how to load external resource with $http.jsonp. Here is my code:

<!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>

And i got error like this:

Error: [$sce:insecurl]

Whats wrong with my code? Thank you.


Solution

  • Working fine by updating your angular version from 1.6.4 to 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>