Last active
January 23, 2017 16:54
-
-
Save mhawila/db415fca85e7356164373c0001d18bde to your computer and use it in GitHub Desktop.
Using $http.jsonp to bypass browser's CORS policy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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