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 & ".")  
%>

Related Topics

Getting and setting content

Retrieving and setting text, values and HTML is the simplest aspect of manipulation of DOM. It seems same but they’re not. There are text(), html() and val() methods. jQuery html() method Using the...

4 minutes read.

jQuery Remove

Using the jQuery, it is easy to remove the existing HTML content. There are two jQuery methods to remove the content: remove() empty() jQuery remove() Method The jQuery remove() method is used to remove the...

2 minutes read.

Introduction to DOM manipulation

Manipulation of DOM is one of the most important aspects of jQuery and thereby JavaScript. DOM stands for Document Object Model. It is a manipulation of interacting and representing with...

1 minute read.

jQuery Events

jQuery event is used to show a moment when something happens. jQuery is used to set up event-driven responses on the HTML page elements. These events have often triggered by the...

1 minute read.

jQuery hide/show Effect

jQuery provides a interface for doing various kind of amazing effects like Slide, Fade, Hide, Show, Toggle Effects and Animate. jQuery hide() and show() Using the jQuery show() and hide() methods, you...

2 minutes read.

jQuery Syntax

jQuery syntax is made by using HTML elements. You can perform some action on those elements. jQuery starts with the dollar sign ($) and ends with a semicolon (;). Basic syntax...

1 minute read.

jQuery CSS() Method

Using the jQuery css() method, you can set or return the one or more style properties for the selected elements. Here are the two ways to provide jQuery css() method: Set a...

2 minutes read.

jQuery Tutorial

jQuery tutorial provides deep knowledge of jQuery technology for both beginners and professionals. In this jQuery tutorial you will learn about example, selectors, traversing, events, effects, jQuery fundamentals, CSS and attributes....

3 minutes read.

jQuery Sliding Effect

jQuery slide is used to slides the element out of the viewport. Here are some slide methods: slideDown() slideUp() slideToggle() jQuery slideDown() slideDown() method of jQuery is used to slide down an element. Syntax: ...

3 minutes read.

jQuery Keyboard Events

When the user press or release a key on the keyboard then the keyboard event fires. Here are some jQuery methods to handle the keyboard events. 1) The keypress() Method: When a keyboard...

2 minutes read.

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...

3 minutes read.

jQuery each() method

jQuery each() method: jQuery contains many processes that make coding a much better and faster work for professionals.  The each() method is one of the earliest method of jQuery. It...

3 minutes read.

jQuery Form Events

When a form control receives or loses focus or when form control value is modified by the user such as by typing text in a text input, select any option...

3 minutes read.

jQuery Stop Effect

The jQuery stop() method is used to stop the jQuery animation or effect before it is finished. The jQuery stop() method works for all jQuery effects like: custom animations, fading and...

1 minute read.

jQuery Selectors

When you use the JavaScript then read and modify the content of the page is the very common task. While doing this you want to find the elements that you...

3 minutes read.

jQuery Add

Using the jQuery, it is easy to add new content. Here are four jQuery methods to add new content: append() prepend() after() before() jQuery append() Method The jQuery append() method is used to insert the content...

4 minutes read.

jQuery Fading Effect

jQuery Fade Using jQuery, you can fade elemnts in and out of visibility. You can change the opacity level of elements from invisible state to visible state or visible state to...

6 minutes read.

jQuery Mouse Events

When the users move the mouse pointer or click some element then the mouse event fires. To handle the mouse events here are some jQuery methods: 1) The click() Method An event...

2 minutes read.

How to use jQuery

If you want to use jQuery then firstly include it on the pages where you wish to take advantage of it. To do this you have to download the jQuery....

2 minutes read.

Introduction to AJAX

AJAX stands for Asynchronous JavaScript and XML. In AJAX, the data is loaded in the background and displayed on the webpage. In this process, you don't need to reload the...

1 minute read.