jQuery.get()

jQuery.get( url, [ data ], [ success(data, textStatus, jqXHR) ], [ dataType ] ) Return: jqXHR

Description: Load data via server HTTP GET request.

  • version added: 1.0jQuery.get( url, [ data ], [ success(data, textStatus, jqXHR) ], [ dataType ] )

    url A string containing the URL to send the request

    data The Key/value parameter that sends the request to the server

    success(data, textStatus, jqXHR) Callback function that is executed when the request is successful.

    dataType The expected data type returned from the server. Default: Intelligent guess (xml, json, script, or html).

This is an abbreviation for Ajax functionality, which is equivalent to:

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

A successful callback function is a response based on the MIME type, via the returned data including an XML root node, string, JavaScript file, or JSON object. It is also a reaction through the status of the text.

In jQuery 1.5, the success callback also passes through the "jqXHR" object (in jQuery 1.4, it's through the XMLHttpRequest object). However, since JSONP forms and cross-domain GET requests do not use XHR, in these cases, the success of (j)XHR and textStatus callback parameter passing is uncertain.

Most implementations will specify a successful handler:

$.get('ajax/test.html', function(data) {
  $('.result').html(data);
  alert('Load was performed.');
});

The entire HTML code snippet requested by this example is inserted into the page.

jqXHR object

In jQuery 1.5, all jQuery Ajax methods return the superset XMLHTTPRequest object. This jQuery XHR object, or "jqXHR," is returned via the $.get() convention's interface implementation, giving it all its properties, methods, and conventions (see Deferred object for more information). For convenience and consistency, the callback name uses $.ajax(), which provides.error().success() and.complete() methods. These methods require a function argument call when the $.ajax() request terminates, and this function receives the same argument as the $.ajax() callback function name.

The convention interface in jQuery 1.5 also enables jQuery Ajax methods, including $.get(), to chain multiple.success(),.complete(), and.error() callbacks to a request, and even assign these requests after the callback may have been completed. If the request has been completed, the callback is triggered immediately.

// Assign handlers immediately after making the request,
  // and remember the jqxhr object for this request
  var jqxhr = $.get("example.php", function() {
    alert("success");
  })
  .success(function() { alert("second success"); })
  .error(function() { alert("error"); })
  .complete(function() { alert("complete"); });

  // perform other work here ...

  // Set another completion function for the request above
  jqxhr.complete(function(){ alert("second complete"); });

Other points to note:

  • Due to browser security limitations, most "Ajax" requirements are based on a policy of the same origin; The request cannot successfully retrieve data from a different domain, subdomain, or protocol.
  • If a jQuery.get () request returns an error code, it will fail quietly unless the script calls globally.ajaxError ()Method. In jQuery 1.5, return by Jquery.get ().error()methodologicaljqXHRObjects can also be used to handle errors.
  • Script and JSONP form requests are not subject to the same origin policy.

Examples:

Example: Request the test.php page, but ignore the return result.

$.get("test.php");

Example: Request the test.php page and send the url parameters (although still ignoring the results returned).

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

Example: Pass the array form dataargumentTo the server (although still ignoring the returned results).

$.get("test.php", { 'choices[]': ["Jon", "Susan"]} );

Example: Alert The result of the data requested from test.php (HTML or XML, depending on the result returned).

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

Example: Alert requests and sends the result of the url parameter data (HTML or XML, depending on the result returned) from test.cgi.

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

Example: Gets the content that test.php's page has returned in JSON format ( "John","time"=>"2pm")); ? >).

$.get("test.php", { "func": "getNameAndTime" },
   function(data){
     alert(data.name); // John
     console.log(data.time); //  2pm
   }, "json");
jQuery 1.6 API Chinese version edited and revised by Script House (June 2011)