3 questions about the JSON-P format

I need to write a script that will be hosted on different domains. This script should get information from my server.

So a stackoverflow user told me that I need to use the JSON-P format , which after researching is what I'm going to do. (the data represented in JSON-P is meant to display some information hosted on my server in another website)

  • How do I output JSON-P from my server? This is the same as the function json_encode

    fromPHP

  • How to create a tree template for JSON-P output (you know, for example: ({"name": "foo", "id": "xxxxx", "blog": " http://xxxxxx.com "}); can do I steal this from my XML output? ( http://bit.ly/9kzBDP )

  • Every time a visitor views the website that my widget is on, it will make a request to my server, requesting JSON-P data to display on the client side. This will significantly increase the CPU load (1 visitor to the site who will have a SQL script = 1 query on my server to output the data), so there is some way to 'cache' the output of the JSON-P information to only update it once or twice per day and save it to a "file" (in what extension?).

BUT, on the other hand , I would say that requesting JSON-P data directly (without caching) is a plus, because the websites that will integrate the script only want to display THEIR information , not all data. So, creating a script with something like this:

jQuery.getJSON("http://www.something.com/json-p/outpout?filter=5&callback=?", function(data) {
................);
});

      

Where filter = information the website wants to display.

  • Bonus question: do I need to use a JS framework to read JSON-P? or JS, without any frameworks to include in the page, can it do this?

What do you think?

Many thanks ;)

+2


a source to share


2 answers


JSONP request

$.ajax({
  type: "get",
  dataType: "jsonp",
  url: "mothership.com/widget_data",
  data: {whatever: "here", requested_from: "someonesblog.com" },
  cache: true,
  success: function(data, textStatus, XMLHttpRequest){
    // respond to ajax call here

    // debug: make sure your data is getting loaded properly
    console.log(data);

    // do other necessary things here
  }
});

      



JSON encoding and output from server

<?php
  header("Content-Type: application/json");

  # You can access data from the jQuery.ajax() call with $_GET here

  $data = array(
    "name" => "foo",
    "id"   => 1234,
    "blog" => $_GET["requested_from"]
  );
  echo $_GET["callback"] . "(" . json_encode($data). ");" ;

  # this will output something like
  # jsonp1255285823({"name":"foo","id":1234,"blog":"someonesblog.com"});

  exit;
?>

      

+2


a source


You can use a function json_encode

to get a string representation of a JSON object and then add a shim to it, i.e .:

$json = json_encode($myObj);
echo $callback . "(" . $json . ");";

      

Most JSON structures closely copy XML structures and naming conventions. The only nodes with no children are properties, duplicate XML nodes are JSON arrays, and nodes with children are objects. See http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=396 for a decent, parallel view of data presented in both formats.



When it comes to caching data on the server, most of the caching methods can get a little overcrowded if your traffic isn't too heavy. See this answer to one of my own questions and this answer recommending memcached if you still insist you need it. You can use the client cache control header expires

to make sure the client only fetches the data once even on multiple pages / updates.

Bonus answer: The beauty of JSON-P is that you don't need a library to parse it. The format is pure javascript and adding JSON-P to the page is as easy as adding a script to the page:

<script 
   type="text/javascript" 
   src="http://myurl.com/jsonp.php?callback=test&filter=5">
</script>

      

+2


a source







All Articles