Angular JSONP returning JSON_CALLBACK not defined error

I am trying to pull data through JSONP using angular. I was puzzled by why it would not work in certain situations.

I managed to successfully use this sample JSONP:

https://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Brian

But when copying to a bucket on S3, I keep getting this error:

Uncaught ReferenceError: JSON_CALLBACK is not defined

The file is publicly accessible and I can access it simply using $.ajax but not $http.jsonp

I tried changing the MIME type for the json file to all of the following:

  • applications / json
  • application / javascript
  • application / x-javascript
  • Text / Plain
  • text / javascript

None of them allowed me to successfully execute the $http.jsonp function

+7
angularjs jsonp
source share
5 answers

Okay, so this is very strange, but in the end I was able to work it by changing the callback shell in JSONP from JSON_CALLBACK(.......) to angular.callbacks._0 , because the angular callback function tried to call instead of JSON_CALLBACK ... very strange behavior.

+8
source share

Unable to see your code, I can show you what worked for me a few days ago when working with JSONP and angular:

 controller('YourCtrl', ['$scope', '$http', function($scope, $http) { var url = "https://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Brian" $http.jsonp(url) .success(function(data) { $scope.greeting = data; }); }]) 

Then in the view something like this should work:

 <div ng-controller="YourCtrl"> {{greeting.name}} <br /> {{greeting.salutation}} </div> 

If this does not answer your question, try pasting into your code.

+1
source share

Brian, I recognized a method to make sure it detects ?callback=JSON_CALLBACK . When you request a URL, instead of putting it all inside, you can write it that way and it detects 100%:

 $http({ method: 'JSONP', url: 'https://angularjs.org/greet.php' + '?callback=JSON_CALLBACK' + '&name=Brian"' }); 
0
source share

The problem is your answer "Rest" or in the json http file on the bucket, you need to dynamically with the get (callback) parameter.

You need to define a callback parameter and go to json to get jsonp .

Example:

 1) http://example.com?callback=JSONP_CALLBACK 2) http://example.com?callback=OtherCallback 3) http://example.com?callback=JSONP_CALLBACK_OTHER 

get the GET callback parameter and do it like this:

 1) JSONP_CALLBACK({YOUR REST INFO}) 2) OtherCallback({YOUR REST INFO}) 3) JSONP_CALLBACK_OTHER({YOUR REST INFO}) 
0
source share

Change your response from server to

 angular.callbacks._0 ({"name":"Super Hero","salutation":"Namaste","greeting":"Namaste Super Hero!"}); 

instead

 JSON_CALLBACK ({"name":"Super Hero","salutation":"Pryvitannie","greeting":"Pryvitannie Super Hero!"}); 
-one
source share

All Articles