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?

4
  • Your code is a deferred Anti-pattern. Where did you get this code example? Commented Apr 3, 2018 at 17:00
  • I wrote this code . Can you help for this issue? Commented Apr 3, 2018 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? Commented Apr 3, 2018 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. Commented Apr 4, 2018 at 19:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.