Reference: jQuery get method

Get data from the server using a HTTP GET request.

Parameters

  1. URL - The URL to get the JSON data from the server.
  2. data - Optional JSON Object containing key / value pairs to send with the request.
  3. calback - Optional function that is called when the request is complete.
  4. type - Optional The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).

Callback

The callback has the following parameters:

  1. responseText - The HTML returned from the server if the request succeeds.
  2. textStatus - The status of the response.
  3. jqXHR - XMLHttpRequest object.

Syntax

$.getJSON(URL, data, callback, type);

Example getting HTML data

Get the viewContent.html page from the server and load the contents into the element with an id of "view".

$.get("/viewContent.html", function(data) {
  $("#view").html(data);
});

Example getting JSON data

Get a list of employees from the server.

$.get("/employee/list", function (list) {
  for (var idx = 0; idx < list.length; idx++) {
    console.log(JSON.stringify(list[idx]));
  }
});
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License