13

What do I need to on the server side to allow someone to get data from that server using JSONP. And what do I need to do on the user side as well? I want to use JSONP as an alternative to an XMLHttpRequest.

It won't work out of my Firefox extension, because of the same-origin policy. So, people recommended JSON, but I am pretty lost after searching for tutorials and guides on the internet.

Thanks for the help!

Jacques Blom
  • 1,794
  • 5
  • 24
  • 42

3 Answers3

13

Assuming your server is running PHP, you just need to add 'callback' GET request.

<?php header('content-type: application/json; charset=utf-8');
$data = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); 
echo $_GET['callback'] . '('.json_encode($data).')';

And on client side (using jQuery):

$.ajax({url: 'http://site.com/data.php', dataType:'jsonp'});

The PHP code above is just for demo, don't forget to sanitise $_GET['callback']

That said, if your issue just the same origin policy, you'll probably just need to allow cross-origin from the server side, and everything should work.

marko
  • 1,721
  • 15
  • 25
  • Thanks so much! The CORS solution fixed me up. – Jacques Blom Mar 01 '12 at 18:34
  • Just downvoted bc this answer assumes php, and therefore I prefer T.J. Crowder's answer below. – Steven2163712 May 17 '17 at 08:03
  • @Steven2163712 if you read beyond the title, OP problem is actually CORS, regardless of the language. While TJ's answer is more generic (IMO php is generic enough), it doesn't solve OPs problem. – marko May 17 '17 at 18:32
7

On the server side, all you have to set up is a web resource (e.g., page) that accepts a GET request and returns the data using the JSON-P convention, which is:

callback({"data": "here"});

...where the function name ("callback" in that example) is usually taken from one of the query string parameters (by convention, the parameter "callback"), and the data is JSON text (although technically it could be anything that's valid in a JavaScript object literaly, the convention with JSON-P is to restrict yourself to what's valid in JSON). So for instance, let's say that the request looked like this:

http://example.com/foo.php?callback=bar

That calls the page foo.php (doesn't have to be PHP, can be any dynamic server-side system), telling it that the function we want it to call is "bar". Our response would be:

bar({"data": "here"});

On the client side, you have to add a script element to the page dynamically, and also add the callback function that will get triggered by the JSON-P response. Usually you want to give that function some random name, and remove it when you're done.

Here's a complete example as an answer to another question here on Stack Overflow. You may have to adapt it slightly for use in a Firefox add-on, but the concepts are the same.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

jsonp is json with a wrapper, so you can fake ajax requests to another server by dynamically inserting new <script> tags, with src's pointing at the other server. The wrapper essentially makes the jsonp return stuff be a valid javascript function call that can be executed to extract the standard json data within.

Generally, in an insecure 'just to demo' version, you'd have something like this:

function unwrap_jsonp(data) {
    eval(data);
}

The remote server would return the following literal text:

unwrap_json("{'this':'is','sparta':'!'}");

Note that this is literal Javascript plaintext code, which is executed and "unwraps" the embedded JSON string back to a native javascript data structure.

Most JSONP services allow specifying an extra parameter via the query string to name the handler function you want to wrap the response in, e.g.

http://example.com/getjsonp.php?callback=unwrap_json
Marc B
  • 356,200
  • 43
  • 426
  • 500