jQuery Get()/Post() Methods

The jQuery get() and post() methods is used to send a HTTP request to a page and get the result back from the web server. GET and POST are the two common methods that are used for a request-response between a client and server. GET - Get is used to request the data from a specified resource. POST - Post is used to submit data to be processed to a specified resource. Get and Post both are used to get some data from the server but get can be returned cached data and post never caches data and post method is often used for sending data along with the request. Both are pretty much identical, apart from one major difference. Both $.get() and $.post() methods makes AJAX requests but $.get() method used the HTTP GET request and $.post method used the HTTP POST request for AJAX request.

jQuery $.get() Method

The jQuery get() method is used to send the asynchronous http GET request to the server and retrieve the data. Syntax:
$.get(URL,callback)
URL: URL parameter specifies the URL from which you want to retrieve the content. callback: The callback parameter is the name of a function to be executed when request succeed. It is an optional parameter.

Example:

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
<script>  
$(document).ready(function(){  
    $("button").click(function(){  
        $.get("demo_test.asp", function(data, status){  
            alert("Data: " + data + "\nStatus: " + status);  
        });  
    });  
});  
</script>  
</head>  
<body>  
<button>Send an HTTP GET request to a page and get the result back</button>  
</body>  
</html>
Try Now The first parameter of jQuery $.get() method is the URL ("demo_test.asp") that you wish to request. callback function is the second parameter. Content of the page request exists in the callback first parameter and status of the request holds in the second callback parameter. Here is how the ASP file ("demo_test.asp") looks like:
<%  
response.write("This is some text from an external ASP file.")  
%>

jQuery $.post() Method

The $.post() method is used to request the data from the server using an HTTP POST request. Syntax:
$.post(URL,data,callback);
URL: URL parameter specifies the URL from which you want to retrieve the content. data: It specifies the data to be sent with request to the server. It is an optional parameter. callback: The callback parameter is the name of a function to be executed when request succeed. It is an optional parameter.

Example:

<!DOCTYPE html>  
<html>  
<head>   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
<script>  
$(document).ready(function(){  
    $("button").click(function(){  
        $.post("demo_test_post.asp",  
        {  
          name: "Mickey Mouse",  
          city: "California"  
        },  
        function(data,status){  
            alert("Data: " + data + "\nStatus: " + status);  
        });  
    });  
});  
</script>  
</head>  
<body>  
<button>Send a request of HTTP Post to a page and get the result back</button>  
</body>  
</html>
Try Now The first parameter of jQuery $.post() method is the URL ("demo_test.asp") that you wish to request then pass some data to send along with the request. The parameters are reads, processed and returned as result by the asp script in "demo_test_post.asp". callback function is the third parameter. Content of the page request exists in the callback first parameter and status of the request holds in the second callback parameter. Here is how the ASP file ("demo_test_post.asp") looks like:
<%  
dim fname,city  
fname=Request.Form("name")  
city=Request.Form("city")  
Response.Write("Dear " & fname & ". ")  
Response.Write("Hope you live well in " & city & ".")  
%>