1

I am working on Angular 1.6.9. I have used jsonp method to get my response. My response is callback function. I am using .success() function in angular 1.4.9 and I am able to parse my Jsonp callback response successfully. Here I have attached my working js fiddle. http://jsfiddle.net/sathees552/m7hh3362/

I am using angular 1.6.9, but since .success() method is deprecated, I am using .then() function instead, as well as $sce.trustAsResourceUrl(url). But I am no longer able to parse my response. Its giving below error

Uncaught ReferenceError: projectInstance is not defined

I have defined projectInstance in my controller, still it's not recognized. But the same method I was using in angular 1.4.9 was working fine. Here I have attached my plunker link which is not working https://plnkr.co/edit/TidicooyborGTGHvUzJK?p=preview

My Html code:

<div ng-app="myapp" ng-controller="personCtrl">
     <button ng-click="doRequest()">Make JSONP request</button>
  </div>

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', function($http, $scope, $sce) {
      $scope.doRequest = function() {
          //creating 
          var 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: 'jsonpCallback'
          }).then(function(data) {
              eval(data)
          }, function(error) {
              console.log(error);
              return error;
          })
      };
  }]);

My Response :

projectInstance.jsonpCallback("getActiveProduct",{"totalRecords":null,"projectSnapShot":[{"projectId":"333333","projectNumber":"123456","projectStatus":"Active"}]})
Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34
  • `.then()` resolves your promise and [returns **an object**](https://docs.angularjs.org/api/ng/service/$http#$http-returns) with your data as one of the values, so you need `.then(function(response) { eval(response.data)` – Aleksey Solovey Apr 06 '18 at 10:36
  • I have tried its not working still it's throwing same error. i have attached my plunker link top . Can you please check that – user3760261 Apr 06 '18 at 10:48

0 Answers0