I am working on AngularJS 1.6.9 JSONP . My api endpoint gives projectInstance.jsonpCallback function as a response. Inside factory method i had created same callback function and i got the response. But after getting successful response its directly triggering .error() function also. I got my response but still i cant able to return that response from factory to my controller. i got console error like below
{data: false, status: 404, headers: ƒ, config: {…}, statusText: "error", …}
How to solve this issue ? How to return JSONP callback result from factory to controller?
I have attached my working plunker link below
[https://plnkr.co/edit/TidicooyborGTGHvUzJK?p=preview][1]
Here is my HTML code
<div ng-app="myapp" ng-controller="personCtrl">
<button ng-click="doRequest()">Make JSONP request</button>
</div
Here is my js code
var app = angular.module('myapp', []);
app.config(function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
'self',
'http://www.mocky.io/v2/**'
]);
});
app.controller('personCtrl', ['$http', '$scope', '$sce', 'myService', function($http, $scope, $sce, myService) {
$scope.doRequest = function() {
myService.getData().then(function(data) {
console.log("data", data);
});
};
}]);
app.factory('myService', function($q, $http, $sce) {
return {
getData: function() {
var q = $q.defer();
window.projectInstance = {
jsonpCallback: function(commandName, responseData) {
alert(responseData.projectSnapShot[0].projectId);
}
}
var url = "https://www.mocky.io/v2/5ac61eed4a000061007e065b";
var trustedUrl = $sce.trustAsResourceUrl(url);
$http.jsonp(trustedUrl, {
jsonpCallbackParam: 'projectInstance.jsonpCallback'
}).then(function(data) {
return data.data;
}, function(error) {
console.log("error",error);
return error;
})
return q.promise;
}
}
})
Here is my Response
projectInstance.jsonpCallback("getActiveProduct",{"totalRecords":null,"projectSnapShot":[{"projectId":"333333","projectNumber":"123456","projectStatus":"Active"}]})