jQuery get()

Introduction

Send an HTTP GET request to a page and get a result back:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>//  www . j a  v a  2  s.c o  m
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.get("ajax.txt", function(data, status){
        document.getElementById("demo").innerHTML = 
           "Data: " + data + "<br/>Status: " + status;
    });
  });
});
</script>
</head>
<body>

<p id="demo"></p>
<button>Send an HTTP GET request to a page and get the result back</button>

</body>
</html>

The $.get() method loads data from the server using a HTTP GET request.

$.get(URL,data,function(data,status,xhr),dataType)
Parameter
Optional
Description
URL
Required.
the URL to request
data
Optional.
data to send to server along with the request
function(data,status,xhr)









Optional.









a function to run if the request succeeds
Additional parameters:
data - the resulting data from the request
status - the status of the request
"success",
"notmodified",
"error",
"timeout", or
"parsererror"
xhr - the XMLHttpRequest object
dataType








Optional.








the data type expected of the server response.
By default jQuery performs an automatic guess.
Possible types:
"xml" - An XML document
"html" - HTML as plain text
"text" - A plain text string
"script" - Runs the response as JavaScript, and returns it as plain text
"json" - Runs the response as JSON, and returns a JavaScript object
"jsonp" - Loads in a JSON block using JSONP.

The following list some examples:

//Request "test.php", 
//ignore return results:
$.get("test.php");

//Request "test.php" 
//send some additional data along with the request 
//ignore return results:
$.get("test.php", { name:"CSS", town:"java2s.com" });

//Request "test.php" 
//pass arrays of data to the server 
//ignore return results:
$.get("test.php", { 'colors[]' : ["Red","Green","Blue"] });

//Request "test.php" 
//alert the result of the request:
$.get("test.php", function(data){   alert("Data: " + data); });



PreviousNext

Related