Sunday, June 7, 2015

AJAX call in AngularJS for cross domain service using JSONP

It is very easy to set up AJAX call in AngularJS with $http.get(). Similarly in Angular cross domain request is also very easy using JSONP, below code shows how to set up AJAX call for cross domain using $http.jsonp():

(function () {
    var App = angular.module("AngularCrossDomain", []);
    App.controller("AppController", function ($scope, $http) {
        $http.jsonp('<Your Service URL>?callback=JSON_CALLBACK')
            .success(function (data) {
                console.log("pass");
                $scope.value = data; 
            })
            .error(function (data) {
                console.log("failed");
            });
    });
})();

No comments:

Post a Comment