Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JSONP returning JSON_CALLBACK is not defined error

I am trying to pull data down via JSONP with angular. I've been puzzled as to why it won't work in certain situations.

I've been able to successfully pull in this sample JSONP:

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

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

Uncaught ReferenceError: JSON_CALLBACK is not defined

The file is public and I can access it just fine with $.ajax, but not $http.jsonp

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

  • application/json
  • application/javascript
  • application/x-javascript
  • text/plain
  • text/javascript

None of them have allowed me to make a successfull call through the $http.jsonp function

like image 535
Brian Avatar asked Jul 21 '14 20:07

Brian


2 Answers

Ok, so this is very strange, but I eventually got it to work by changing the callback wrapper in the JSONP from JSON_CALLBACK(.......) to angular.callbacks._0 because that's the callback function angular kept trying to call instead of JSON_CALLBACK... very weird behavior.

like image 123
Brian Avatar answered Sep 25 '22 21:09

Brian


Not being able 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 doesn't answer your question, try pasting in your code.

like image 23
Nader Dabit Avatar answered Sep 24 '22 21:09

Nader Dabit