Explanation of Jquery get/post with Example.

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

The two most 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 $.get() method requests data from the server with an HTTP GET request.

Syntax:

$.get( url, [data], [callback], [type] )

Parameters

  1. url : String containing the URL at which request is to be sent
  2. data : This is an optional parameter that represents key/value pairs that will be sent to the server.
  3. callback: This optional parameter represents the function that is to be executed whenever the data is loaded successfully.
  4. type : This parameter represents type of data to be returned to the callback function i.e “xml”, “script”, “json”, “html”, or “text”.

Example :-

This PHP code is use to get data when below HTML program send the HTTP GET request.

// get.php file
<?php
if( $_REQUEST[“name”] ) {

$name = $_REQUEST[‘name’];
echo “Welcome “. $name;
}

?>

HTML PARTS :-

This HTML code is use to send the HTTP GET request.

<script type="text/javascript">

    $(document).ready(function() {

    $("#driver").click(function(event) {
            $.get(
            "get.php", {
                name: "roshan"
            },
            function(data) {
                $('#stage').html(data);
            });
        });
    });
</script>

Click on the button to load get file

<span id="stage"
    style="background-color:#cc0;">
    Roshan Kumar Jha
</span>

<div>
    <input type="button"
        id="driver"
        value="Get Data" />
</div>

jQuery | post() Method

The post() method in jQuery loads the page from server using POST HTTP request and returns XMLHttpRequest object.

Syntax:

$.post( url, data, callback_function, data_type )

Parameters: 

  • url: It is the required parameter and used to send the request.
  • data: It is optional parameter and it represents key/value pairs of data that will be sent to the server.
  • callback_function: It is optional parameter and it represents a function to be executed when the data is loaded successfully.
  • data_type: It is optional parameter and it represents a type of data to be returned to callback function: xml, html, script, json or text.
  • main.php: This PHP file call in the below example when button pressed.

Example :-

Use post() method and call a PHP file.

// main.php file

<?php

    echo"Hello Roshan!";

?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
 <h2 id="rk">Roshan Kumar jha</h2> 
<button id="submit">
    Click Here!
</button>

<!-- Script to use post() method -->
<script>
    $(document).ready(function() {
        $("button").click(function() {
            $.post("/submit.php", {
                name: "Roshan",
            },

            function(data,status) {
                document.getElementById("rk").innerHTML
                        = data;
                document.getElementById("submit").innerHTML
                        = "Data Passed";
            });
        });
    });
</script>

Related Posts

Optimizing Analytics Workflows with DataOps Tools

In today’s data-driven enterprise, raw data is generated at unprecedented volumes across distributed systems. However, turning that raw data into reliable business intelligence often feels like navigating…

Read More

Start a Blog for Free: Step-by-Step Beginner Guide

Blogging remains one of the most powerful mediums for sharing ideas, building an online presence, and establishing authority in any field. Whether you want to document your…

Read More

The Complete Guide to Free Blog Hosting for Creative Writers

INTRODUCTION In an era dominated by fast-paced social media feeds and endless algorithm updates, long-form content creation continues to stand as the cornerstone of thoughtful communication, personal…

Read More

Top Pediatric Cardiac Hospitals and Expert Heart Specialists

INTRODUCTION Choosing where to undergo cardiac treatment is one of the most significant healthcare decisions a patient or family will ever make. Heart conditions demand swift intervention,…

Read More

Discover How Tool Selection Impacts DataOps Success and Team Productivity

Modern businesses run on data. Every click, sale, and customer interaction generates information that companies use to make decisions. However, raw data is like crude oil; it…

Read More

Strategies to Monitor Multi-Cloud Data Pipelines and Prevent Failures

Introduction Today, data drives almost every major business decision. To keep up with massive amounts of information, organizations no longer rely on just one cloud provider. Instead,…

Read More