Skip to content

Instantly share code, notes, and snippets.

@mhawila
Last active January 23, 2017 16:54
Show Gist options
  • Save mhawila/db415fca85e7356164373c0001d18bde to your computer and use it in GitHub Desktop.
Save mhawila/db415fca85e7356164373c0001d18bde to your computer and use it in GitHub Desktop.
Using $http.jsonp to bypass browser's CORS policy.
(function() {
'use strict';
// This is how you would do it using angular 1 & http service
var app = angular.module('your.module', []);
app.factory('Service', Service);
Service.$inject = ['$http', '$sce'];
function Service($http, $sce) {
var service = {
fetchSomething: fetchSomething,
};
return service;
var fetchSomething = function(callbackName) {
var url = `http://localhost:3000/test?callback=${callbackName}`; //Absolute or relative URL
var trusted = $sce.trustAsResourceUrl(url);
return $http.jsonp(trusted, {jsonpCallbackParam: callbackName });
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment