Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs JSONP not working

I might be missing something here but I can't make this JSONP request work, here is the code:

var url =  'http://' + server + '?callback=JSON_CALLBACK';
$http.jsonp(url)
    .success(function(data){
       console.log('success');
    })
    .error(function () {
      console.log('error')
    });

The request fires ok and I am getting the response (with header Content-Type:application/json) in this format:

    [{"id": "1", "name": "John Doe"},
     {"id": "2", "name": "Lorem ipsum"},
     {"id": "3", "name": "Lorem ipsum"}]

Can you see anything wrong? Maybe the format I should return from the server is not right? Angular fires the error callback without any error message besides the one I set ('error').

like image 564
Bertrand Avatar asked May 02 '13 18:05

Bertrand


People also ask

How to set http request header in AngularJS?

To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g. $httpProvider. defaults. headers. get = { 'My-Header' : 'value' } .

What is$ http in AngularJS?

$http is an AngularJS service for reading data from remote servers.

What is JSONP in angular?

JSONP is a method of performing API requests which go around the issue of CORS . This is a security measure implemented in all browsers that stops you from using an API in a potentially unsolicited way and most API s, including the iTunes API , are protected by it.

What is JSONP callback?

JSON with Padding, or JSONP for short, is a technique that allows developers to get around browsers' same-origin policies by exploiting the nature of the <script> element. The policy prohibits reading any responses made by websites with origins other than those currently in use.


2 Answers

@TheHippo is correct the data should not just be a plain json response. Here is a working example of a JSONP request against a youtube endpoint in AngularJS.

A couple of things to note in this example:

  • Angular's $http.jsonp converts the request querystring parameter from callback=JSON_CALLBACK to callback=angular.callbacks._0.
  • When calling the youtube endpoint I needed to specify to the service that this is a JSONP request by using alt=json-in-script instead of alt=json in the querystring. This was found in their documentation.
  • Compare the results of this url to this one to see the difference between JSON and JSONP response in your browser.
  • Take a look at the Chrome Network Panel in Developer Tools to help compare and troubleshoot with your request/response.

I know this example is very specific but hopefully it helps!

like image 154
Gloopy Avatar answered Oct 18 '22 20:10

Gloopy


JSONP requires you do wrap your data into a JavaScript function call. So technically you return a JavaScript file and not a Json file.

The data returned from server should similar to this:

// the name should match the one you add to the url
JSON_CALLBACK([
    {"id": "1", "name": "John Doe"},
    {"id": "2", "name": "Lorem ipsum"},
    {"id": "3", "name": "Lorem ipsum"}
]);

Edit: If some one else stumbles across problems with angular's JSONP please also read this answer to this question, it contains usefull informations about how angular handles the actual callback function.

like image 16
TheHippo Avatar answered Oct 18 '22 19:10

TheHippo