jQuery - AJAX get() and post() Methods
jQuery get() and post() methods are used to request data from the server via HTTP GET or POST requests.
HTTP Requests: GET vs POST
Two commonly used methods for a request-response between a client and server are: GET and POST.
- GET - Requests data from a specified resource
- POST - Submits data to be processed to a specified resource
GET is essentially used to retrieve data from the server. Note: GET methods may return cached data.
POST can also be used to get some data from the server. However, the POST method never caches data, and is often used to send data along with the request.
To learn more about GET and POST and the differences between the two methods, read our HTTP Methods - GET vs POST.
jQuery $.get() Method
The $.get() method requests data from the server with an HTTP GET request.
Syntax:
$.get(URL,callback);
or
$.get( URL [, data ] [, callback ] [, dataType ] )
- URL: The URL string to which the request is sent.
- data: Optional, a string or key/value pairs that are sent to the server with the request.
- callback: Optional, a function to be executed if the request succeeds.
- dataType: Optional, the type of data expected from the server. Default: Intelligent guess (xml, json, script, or html).
The following example uses the $.get() method to retrieve data from a file on the server:
Example
$("button").click(function(){
$.get("demo_test.php", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
The first parameter of $.get() is the URL we wish to request ("demo_test.php").
The second parameter is the callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.
Tip: This PHP file ("demo_test.php") might look like this:
demo_test.php File Code:
<?php
echo 'This is data read from a PHP file.';
?>
jQuery $.post() Method
The $.post() method sends data to the server with an HTTP POST request.
Syntax:
$.post(URL,callback);
or
$.post( URL [, data ] [, callback ] [, dataType ] )
- URL: The URL string to which the request is sent.
- data: Optional, a string or key/value pairs that are sent to the server with the request.
- callback: Optional, a function to be executed if the request succeeds.
- dataType: Optional, the type of data expected from the server. Default: Intelligent guess (xml, json, script, or html).
The following example uses the $.post() method to send data along with the request:
Example
$("button").click(function(){
$.post("/try/ajax/demo_test_post.php",
{
name: "tutorialpro.org",
url: "http://www.tutorialpro.org"
},
function(data, status){
alert("Data: \n" + data + "\nStatus: " + status);
});
});
The first parameter of $.post() is the URL we wish to request ("demo_test_post.php").
Then we send data along with the request (name and url).
The PHP script in "demo_test_post.php" reads these parameters, processes them, and returns a result.
The third parameter is the callback function. The first callback parameter holds the content of the page requested, and the second parameter holds the status of the request.
Tip: This PHP file ("demo_test_post.php") might look like this:
demo_test_post.php File Code:
<?php
$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '';
$url = isset($_POST['url']) ? htmlspecialchars($_POST['url']) : '';
echo 'Site name: ' . $name;
echo "\n";
echo 'URL: ' . $url;
?>