1

I am working on angularjs services. I got some callback response from my API endpoint. I am using angular jsonP to fetch callback response I have used jsonpCallbackParam: 'jsonpCallback' to parse my callback response. But this jsonpCallback function is not triggered. I have attached my response below.

projectInstance.jsonpCallback("getActiveProduct",{"totalRecords":null,"projectSnapShot":[{"projectId":"333333","projectNumber":"123456","projectStatus":"Active"}]})

Here is my service code:

getActiveProduct: function() {
var deferred = $q.defer();
var url = "myserver/getproject.json"
projectInstance={};
$http.jsonp(url, {
    jsonpCallbackParam: 'jsonpCallback',
    data: ''
}).then(function(data) {
    deferred.resolve(data.data);
}, function(error) {
    console.log(error);
    return deferred.reject(err);
});
return deferred.promise;}

Here is my jsonpCallback function

jsonpCallback: function(commandName, responseData) {
    if (commandName == "getActiveProduct") {
        var jsonObject = responseData;
        var projectArray = [];
        angular.forEach(jsonObject.projectSnapShot, function(index, item) {
            projectArray.push(item);
            console.log(projectArray);

        });
    }

Here i attached my service end point .It automatically attach angular.callbacks._0

myserver/getproject.json?callback=angular.callbacks._0

I have attached my console error below

Uncaught TypeError: Cannot read property 'jsonpCallback' of null

Its throws error response like

{data: false, status: 404, headers: ƒ, config: {…}, statusText: "error", …}

I got response in network Tab . But while parsing in front end because of this JsonpCallback its throws console error.How to fix this issue?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • Your code is a [deferred Anti-pattern](https://stackoverflow.com/questions/30750207/is-this-a-deferred-antipattern). Where did you get this code example? – georgeawg Apr 03 '18 at 17:00
  • I wrote this code . Can you help for this issue? – user3760261 Apr 03 '18 at 17:12
  • The server is not following proper jsonp protocol. `$http.jsonp` expects the callback to be specified by a url parameter. Do you have access to the server to fix it? – georgeawg Apr 03 '18 at 18:03
  • The `$http.jsonp` service expects the callback to be specified by a url query parameter. Since this API doesn't accept a callback parameter, do the access with JavaScript. See [Making and handling JSONP request using JavaScript](https://stackoverflow.com/questions/5907777/making-and-handling-jsonp-request-using-javascript). – georgeawg Apr 04 '18 at 19:28

0 Answers0