AJAX Sending POST Request In Hindi

📔 : AJAX 🔗

पिछले topic में आपने पढ़ा कि किस तरह से JavaScript AJAX में GET request send करते हैं ,इस topic में आप पढ़ेंगे कि JavaScript में POST request कैसे send करते हैं।


actually GET request ज्यादातर तब use करते हैं जब हमें request के साथ server पर बहुत कम data send करना हो , ज्यादा data send करने के लिए या file upload करने के लिए POST request ही use करते हैं , क्योंकि GET request के साथ file upload नहीं की जा सकती है।


POST request के साथ आप आसानी से file uploading या data को आसानी से send कर सकते हैं , इसी वजह से GET request , POST request के comparison में काफी fast होती है।

JavaScript POST Request Example

सबसे पहले एक PHP File बनाते हैं , जहाँ पर POST request को handle कर सकें।

File : handle_request.php

Copy Fullscreen Close Fullscreen
<?php
   echo 'POST Request Received.';
   echo '<br>'; /* for line break */
   echo 'requested data : <pre>';
   print_r($_POST);
?>

HTML JavaScript Code

File : post.html

CopyFullscreenClose Fullscreen
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AJAX POST Request</title>
</head>
<body>
<div id="result">Here result will be show.</div> <br>
<button onclick="send_get_request()">Click Here To Send POST Request</button>
<script type="text/javascript">
function send_get_request()
{
var xhttpreq = new XMLHttpRequest();
xhttpreq.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{
document.getElementById("result").innerHTML = this.responseText;
}
};

/*make formData Object and put value inside it*/
let send_data = new FormData();
send_data.append('name', 'Girish Kumar Shekhawat');
send_data.append('age', 28);
send_data.append('designation', 'Web Developer');
send_data.append('conatct', 'test@gmail.com');
send_data.append('about', 'All about Girish Kumar Shekhawat');

xhttpreq.open("POST", "handle_request.php", true);
xhttpreq.send(send_data);
}
</script>
</body>
</html>

Important
Note , Request handle करने के लिए किसी server-side language और उसे Run करने के लिए Environment की जरूरत होती होती है , Example में PHP use की गयी है , और उसे run करने के लिए XAMPP Control Panel use किया गया है।
Learn PHP Tutorials Now

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers