jQuery: The Write Less, Do More JavaScript Library

Ajax/jQuery.get

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Ajax

jQuery.get( url, [data][callback] )

Load a remote page using an HTTP GET request.
This is an easy way to send a simple GET request to a server without having to use the more complex $.ajax function. It allows a single callback function to be specified that will be executed when the request is complete (and only if the response has a successful response code). If you need to have both error and success callbacks, you may want to use $.ajax.
Arguments:

urlString
The URL of the page to load.
data (Optional)Map
Key/value pairs that will be sent to the server.
callback (Optional)Function
A function to be executed whenever the data is loaded successfully.
function (data, textStatus) {
  // data could be xmlDoc, jsonObj, html, text, etc...
  this; // the options for this ajax request
}

Examples:
Request the test.php page, but ignore the return results.

$.get("test.php");

Request the test.php page and send some additional data along (while still ignoring the return results).

$.get("test.php", { name: "John", time: "2pm" } );

Alert out the results from requesting test.php (HTML or XML, depending on what was returned).

$.get("test.php", function(data){
  alert("Data Loaded: " + data);
});

Alert out the results from requesting test.cgi with an additional payload of data (HTML or XML, depending on what was returned).

$.get("test.cgi", { name: "John", time: "2pm" },
  function(data){
    alert("Data Loaded: " + data);
  });

NameType