Not a Legal JSONP API -- How to get data without CALLBACK parameter
Angular 1.6 - JSONP throws EXCEPTION despite Response with status: 200 Ok for URL
Im trying to grab some data from a JSONP endpoint. It looks like the data is being returned in the response but Angular nonetheless throws an error.
var url = "https://careers.icims.com/jobs-api/"
var trustedUrl = $sce.trustAsResourceUrl(url);
$http.jsonp(trustedUrl, {jsonpCallbackParam: 'jobs'}).then(function(res){
console.log(res); // this is never executed :.(
});
I am getting the following error: Uncaught ReferenceError: jobs is not defined
at jobs-api?jobs=angular.callbacks._0:1
where jobs
refers to my JSONP prefix
Yet the response returns the JSONP script:
Why is this exception being thrown and how can it be cleared? I am on Angular 1.6.0
Not a Legal JSONP API
The API at that URL is not a legal JSONP API.
It can be gotten with a dangerous service:
app.service("dangerousAPI", function($q) {
this.get = get;
function get(funcName, url) {
var dataDefer = $q.defer();
window[funcName] = function(x) {
dataDefer.resolve(x);
}
var tag = document.createElement("script");
tag.src = url;
document.getElementsByTagName("head")[0].appendChild(tag);
return dataDefer.promise;
}
})
Use at your own risk.
The DEMO
angular.module("app",[])
.service("dangerousAPI", function($q) {
this.get = get;
function get(funcName, url) {
var dataDefer = $q.defer();
window[funcName] = function(x) {
dataDefer.resolve(x);
}
var tag = document.createElement("script");
tag.src = url;
document.getElementsByTagName("head")[0].appendChild(tag);
return dataDefer.promise;
}
})
.run(function($rootScope, dangerousAPI) {
var url = "https://careers.icims.com/jobs-api/";
dangerousAPI.get('jobs',url).then(function(data) {
$rootScope.data = data;
})
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
<h1>Dangerous API DEMO</h1>
<pre>{{data | json}}</pre>
</body>