jQuery - AJAX get() and post() Methods


The jQuery get () and post () methods are used to request data from the server via an HTTP GET or POST application.


HTTP Request: 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 used to retrieve (retrieve) certain data from the server. Note: The GET method may retrieve archived data.

POST can also be used to retrieve certain data from the server. However, the POST NEVER method saves data, and is often used for data transmission and request.


jQuery $.get() Method

The $.get() method requests data from the server via the HTTP GET application.

Syntax:


$.get(URL,callback);

The required URL parameter specifies the URL you wish to request.

Optional call parameter is the name of the job to be performed if the application is successful.

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


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

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

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

Tip: Here is what the ASP file looks like ("demo_test.asp"):


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


jQuery $.post() Method

The $.post() method requests data from a server using the HTTP POST application.

Syntax:


$.post(URL,data,callback);

The required URL parameter specifies the URL you wish to request.

The data parameter you select specifies the specific data to be sent along with the request.

Optional call parameter is the name of the job to be performed if the application is successful.

The following example uses the $.post() method to submit specific data and request:


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

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

Then we transfer some data to send it along with the request (name and city).

ASP text in "demo_test_post.asp" reads the parameters, processes them, and returns the result.

The third parameter is the re-driving function. The first redirect parameter contains the content of the requested page, and the second redirect parameter contains the request status.

Tip: Here is what the ASP file looks like ("demo_test_post.asp"):


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