Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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

share|improve this question

3 Answers 3

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.

share|improve this answer
    
What is the version of angular.js you are using? – runTarm Jul 22 '14 at 16:54
2  
The $http.jsonp will replace the JSON_CALLBACK in url with the angular.callbacks.{{callbackId}} automatically. For some reason, it failed to do so in your case. – runTarm Jul 22 '14 at 16:59

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.

share|improve this answer
    
Plunkr – Nader Dabit Jul 21 '14 at 22:07
    
That's exactly what I'm doing. – Brian Jul 22 '14 at 1:36

Brian I found out a method to make sure it detects ?callback=JSON_CALLBACK. When you request for the url, instead of putting the whole thing inside, you can write it this way, and it detects 100%:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.