Loading Please Wait...

Logo Lynxsia IT Solutions

jQuery Ajax Get And Post

JQ AJAX Get And Post

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

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 basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data.

POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.

jQuery $.get() Method

The jQuery $.get() method requests data from the server with an HTTP GET request.

Syntax
					 
        
          $.get(URL,callback);
        
      

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

The optional callback parameter is the name of a function to be executed if the request succeeds.

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

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

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

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

Tip: Here is how the PHP file looks like ("demo_test.php"):

					 
        
          <?php
            echo "This is responce from external php file";
          ?>
        
      
jQuery $.post() Method

The jQuery $.post() method requests data from the server with an HTTP POST request.

Syntax
					 
        
          $.post(URL,data,callback);
        
      

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

The optional data parameter specifies some data to send along with the request.

The optional callback parameter is the name of a function to be executed if the request succeeds.

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

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

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

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

The PHP script in "demo_test_post.php" reads the parameters, processes them, and returns a result.

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

Tip: Here is how the PHP file looks like ("demo_test_post.php"):

					 
        
          <?php
            $name = $_POST['name'];
            $city = $_POST['city'];
            echo "Dear " . $name ", ";
            echo "Hope you live well in " . $city;
          ?>
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved