Using JSONP in AngularJs

Today, I have tracking down an “Access Denied” error when I came across an AngularJS that was making a Cross-Domain request. I assumed that it have a limitation with CORS (Cross-Origin Resource Sharing), so I  have found solution hear.

 

var app = angular.module('demoapp', ['utf8-base64']);

app.controller('democtr', ['$scope', '$http', 'base64',
    function mydemoCtrl($scope, $http, base64) {
        $scope.realTimeData;

        var url = "http://example.com/api/url";
        var turl = base64.encode(url);
        var api_url = "http://example.com/demo/api/getdata.php" + "?callback=JSON_CALLBACK&url="+turl;

        $http.jsonp(api_url).success(function(data){
            $scope.realTimeData = data;
            // do this anything with data
        });
    }
]);

14. June 2015 by nuruddin AngularJS, Javascript Leave a comment 2,269 views

Leave a Reply