Social Networks

The jQuery.getJSON( url, [data], [callback] ) method loads JSON data from the server using a GET HTTP request.

Description: Load JSON-encoded data from the server using a GET HTTP request.

urlA string containing the URL to which the request is sent.
dataA map or string that is sent to the server with the request.
success(data, textStatus, jqXHR)A callback function that is executed if the request succeeds.
This is a shorthand Ajax function, which is equivalent to:


$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

Data that is sent to the server is appended to the URL as a query string. If the value of the data parameter is an object (map), it is converted to a string and url-encoded before it is appended to the URL.

Most implementations will specify a success handler:


$.getJSON('ajax/test.json', function(data) {
  var items = [];

  $.each(data, function(key, val) {
    items.push('
	
  • ' + val + '
  • '); }); $('
      ', { 'class': 'my-new-list', html: items.join('') }).appendTo('body'); });

    This example, of course, relies on the structure of the JSON file:

    
    {
      "one": "Singular sensation",
      "two": "Beady little eyes",
      "three": "Little birds pitch by my doorstep"
    }

    Using this structure, the example loops through the requested data, builds an unordered list, and appends it to the body.

    The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method. It is also passed the text status of the response.

    As of jQuery 1.5, the success callback function receives a “jqXHR” object (in jQuery 1.4, it received the XMLHttpRequest object). However, since JSONP and cross-domain GET requests do not use XHR, in those cases the jqXHR and textStatus parameters passed to the success callback are undefined.

    Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript’s object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.

    JSONP
    If the URL includes the string “callback=?” (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.

    Example: Loads the four most recent cat pictures from the Flickr JSONP API.

    Example: Loads the four most recent cat pictures from the Flickr JSONP API.

    <!DOCTYPE html>
    <html>
    <head>
      <style>img{ height: 100px; float: left; }</style>
      <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
      <div id="images">
    
    </div>
    <script>
    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
      {
        tags: "cat",
        tagmode: "any",
        format: "json"
      },
      function(data) {
        $.each(data.items, function(i,item){
          $("<img/>").attr("src", item.media.m).appendTo("#images");
          if ( i == 3 ) return false;
        });
      });</script>
    
    </body>
    </html>

    Example: Load the JSON data from test.js and access a name from the returned JSON data.

    $.getJSON("test.js", function(json) {
       alert("JSON Data: " + json.users[3].name);
     });

    Example: Load the JSON data from test.js, passing along additional data, and access a name from the returned JSON data.

    $.getJSON("test.js", { name: "John", time: "2pm" }, function(json) {
        alert("JSON Data: " + json.users[3].name);
        });

    None found.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Allowed Tags:

    <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>