jQuery ajax - get() method

Example

Use AJAX GET requests to change the text of the div element:

$("button").click(function(){
  $.get("demo_ajax_load.txt", function(result){
    $("div").html(result);
  );
);

Try it yourself

Definition and Usage

The get() method loads information via a remote HTTP GET request.

This is a simple GET request feature to replace the complex $.ajax. A callback function can be called when the request is successful. If a function needs to be executed when an error occurs, please use $.ajax.

Syntax

$(selector).get()url,data,success(response,status,xhr),dataType)
Parameters Description
url Required. Specifies which URL to send the request to.
data Optional. Specifies the data sent to the server along with the request.
success(response,status,xhr)

Optional. Specifies the function to be executed when the request is successful.

Additional parameters:

  • response - Contains the result data from the request
  • status - Contains the status of the request
  • xhr - Contains the XMLHttpRequest object
dataType

Optional. Specifies the expected data type of the server response.

By default, jQuery will make an intelligent judgment.

Possible types:

  • "xml"
  • "html"
  • "text"
  • "script"
  • "json"
  • "jsonp"

Detailed Description

This function is a shorthand Ajax function, equivalent to:

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

According to the different MIME types of the response, the returned data passed to the success callback function is also different, which can be XML root element, text string, JavaScript file, or JSON object. You can also pass the text status of the response to the success callback function.

For jQuery 1.4, you can also pass the XMLHttpRequest object to the success callback function.

Example

Request the test.php web page, ignore the returned value:

$.get("test.php");

More Examples

Example 1

Request the test.php web page, send 2 parameters, ignore the returned value:

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

Example 2

Display the returned value of test.php (HTML or XML, depending on the returned value):

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

Example 3

Display the returned value of test.cgi (HTML or XML, depending on the returned value), and add a set of request parameters:

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