Skip to content

Instantly share code, notes, and snippets.

@sajeetharan
Created April 22, 2017 16:49
Show Gist options
  • Save sajeetharan/d69ad52d31e655c33a9c07865648b984 to your computer and use it in GitHub Desktop.
Save sajeetharan/d69ad52d31e655c33a9c07865648b984 to your computer and use it in GitHub Desktop.
jsonp
(function () {
angular.module('myapp', ['ngRoute'])
.config(["$httpProvider", function ($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}])
//define controller and inject $http service as dependency.
.controller('axajCtrl', ['$http', '$scope', function ($http, $scope) {
var url = "http://deals.ownaroof.com/api/webservices/locations_list.json?callback=JSON_CALLBACK";
$http.jsonp(url)
.success(function (data) {
console.log(data.found);
});
}])
//afactory to consume webservices and return data to controllers.
.service('webServices', ['$http', function ($http) {
return {
getLocations: function () {
return $http.get('http://deals.ownaroof.com/api/webservices/locations_list.json').then(function (response) { //wrap it inside another promise using then
return response.data.response.locations; //only return locations
});
}
}
}])
//define controller and inject webServices service as dependency.
.controller('prefferedCtrl', ['webServices', '$scope', function (webServices, $scope, $ngRoute) {
webServices.getLocations().then(function (response) {
$scope.locations = response; //Assign data received to $scope.data
console.log($scope.locations);
});
}])
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment