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 request: GET vs. POST

Two commonly used methods for request-response between the client and server are: GET and POST.

  • GET - Request data from the specified resource
  • POST - Submit the data to be processed to the specified resource

GET is mainly used to obtain (retrieve) data from the server. Note: The GET method may return cached data.

POST can also be used to retrieve data from the server. However, the POST method does not cache data and is often used to send data along with the request.

For more information about GET and POST and the differences between the two methods, please read our HTTP method - GET vs. POST.

jQuery $.get() method

The $.get() method retrieves data from the server using an HTTP GET request.

Syntax:

$.get(URL,callback);

Required URL Parameter specifies the URL you want to request.

Optional callback The parameter is the name of the function to be executed after the request is successful.

The following example uses the $.get() method to retrieve data from a file on the server:

Example

$("button").click(function(){
  $.get("demo_test.asp",function(data,status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});

Try it yourself

The first parameter of $.get() is the URL we want to request ("demo_test.asp").

The second parameter is the callback function. The first callback parameter contains the content of the requested page, and the second callback parameter contains the status of the request.

Tip:This ASP file ("demo_test.asp") looks like this:

<%
response.write("This is some text from an external ASP file.")
%>

jQuery $.post() method

The $.post() method requests data from the server via HTTP POST request.

Syntax:

$.post(URL,data,callback);

Required URL Parameter specifies the URL you want to request.

Optional data Parameter specifies the data to be sent along with the request.

Optional callback The parameter is the name of the function to be executed after the request is successful.

The following example uses $.post() to send data along with the request:

Example

$("button").click(function(){
  $.post("demo_test_post.asp",
  {
    name:"Donald Duck",
    city:"Duckburg"
  ,
  function(data,status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});

Try it yourself

$.post() The first parameter is the URL we want to request ("demo_test_post.asp").

Then we send the data along with the request (name and city).

The ASP script in "demo_test_post.asp" reads these parameters, processes them, and then returns the result.

The third parameter is the callback function. The first callback parameter contains the content of the requested page, and the second parameter contains the request status.

Tip:This ASP file ("demo_test_post.asp") is similar to the following:

<%
dim fname,city
fname=Request.Form("name")
city=Request.Form("city")
Response.Write("Dear " & fname & ". ")
Response.Write("Hope you live well in " & city & ".")
%>

jQuery AJAX Reference Manual

For a complete reference of AJAX methods, please visit our jQuery AJAX Reference Manual.