I am trying to get details from an external API but got stuck in getting the JSON response.
Here is my service code:
angular.module('app').factory('appService', appService);
appService.$inject = ['$http','$sce'];
function appService($http, $sce) {
return {
getData: getData
};
function getData() {
//Rest Headers
var URL = "https://www.westelm.com/services/catalog/v4/category/shop/new/all-new/index.json?callback=JSON_CALLBACK";
var requestHeaders = {
'Content-Type': 'application/json',
'Accept': 'application/json'
};
var trustedUrl = $sce.trustAsResourceUrl(URL);
return $http.jsonp(trustedUrl, {jsonpCallbackParam: 'callback'})
.then(function successCallback(response) {
console.log("succes");
return response.data;
}, function errorCallback(response) {
console.log("error");
console.log(response);
return response.data;
}
);
}
}
I am getting below error message:
Error: [$http:badjsonp] http://errors.angularjs.org/1.6.9/$http/badjsonp?p0=https%3A%2F%2Fwww.westelm.com%2Fservices%2Fcatalog%2Fv4%2Fcategory%2Fshop%2Fnew%2Fall-new%2Findex.json%3Fcallback%3DJSON_CALLBACK
But the API is running fine. How can I hit this API to get the data?
Updated code based on given answer:
angular.module('app').factory('appService', appService);
appService.$inject = ['$http','$sce'];
function appService($http, $sce) {
//Binding
return {
getData: getData
};
function jsonp_callback(data) {
console.log("callback");
console.log(data);
return true;
}
function getData() {
//Rest Headers
var URL = "https://www.westelm.com/services/catalog/v4/category/shop/new/all-new/index.json";
var requestHeaders = {
'Content-Type': 'application/json',
'Accept': 'application/json'
};
$sce.trustAsResourceUrl(URL);
return $http.jsonp(URL, {
jsonpCallbackParam: 'jsonp_callback'
})
.then(function(response) {
console.log("succes");
console.log(response);
});
}
}
Now I am getting error as :
Error: [$sce:insecurl] http://errors.angularjs.org/1.6.9/$sce/insecurl?p0=https%3A%2F%2Fwww.westelm.com%2Fservices%2Fcatalog%2Fv4%2Fcategory%2Fshop%2Fnew%2Fall-new%2Findex.json