HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
The jQuery get() and post() methods are used to request data from the server with an HTTP GET or POST request
Two commonly used methods for a request-response between a client and server are: GET and POST.
GET is basically used for just getting (retrieving) some data from the server. Note: The GET method 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, please read our HTTP Methods GET vs POST chapter.
The $.get()
method requests data from the server with an HTTP GET request.
The required URL parameter specifies the URL you wish to request.
The optional callback parameter is the name of a function to be executed if the request succeeds.
The following example uses the $.get()
method to retrieve data from a file on
the server:
$("button").click(function(){
$.get("demo_test", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
The first parameter of $.get()
is the URL we wish to request ("demo_test").
The second parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.
The $.post()
method requests data from the server using an HTTP POST request.
The required URL parameter specifies the URL you wish to request.
The optional data parameter specifies some data to send along with the request.
The optional callback parameter is the name of a function to be executed if the request succeeds.
The following example uses the $.post()
method to send some data along with the
request:
$("button").click(function(){
$.post("demo_test_post",
{
name: "Donald Duck",
city: "Duckburg"
},
function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
The first parameter of $.post()
is the URL we wish to request ("demo_test_post").
Then we pass in some data to send along with the request (name and city).
The ASP script in "demo_test_post" reads the parameters, processes them, and returns a result.
The third parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.
For a complete overview of all jQuery AJAX methods, please go to our jQuery AJAX Reference.