WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

jQuery - AJAX get() and post() Methods

The 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 common methods for request-response on the client and server are: GET and POST.

  • GET -request data from the specified resource
  • POST -submit data to be processed to the specified resource

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

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

To learn more about GET and POST and the differences between the two methods, please read our HTTP Methods GET versus POST chapter.


jQuery $ .get () method

$. get () method requests data from the server via an HTTP GET request.

Syntax:

$.get(URL,callback);

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

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

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: " + information + "\nStatus: " + status);
    });
});
Try it Yourself »

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

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

Tip: This PHP file ("demo_test.php") looks like this:

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

jQuery $ .post () method

The $ .post () method submits data to the server via an HTTP POST request.

Syntax:

$.post(URL,data,callback);

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

The optional data parameter specifies the data sent with the request.

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

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: " + information + "\nStatus: " + status);
    });
});

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

We then send the data along with the request (name and url).

The PHP script in

"demo_test_post.php" reads these parameters, processes them, and returns the results.

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

Tip: This PHP file ("demo_test_post.php") looks like this:

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