If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
पिछले 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 होती है।
सबसे पहले एक PHP File बनाते हैं , जहाँ पर POST request को handle कर सकें।
File : handle_request.php
<?php
echo 'POST Request Received.';
echo '<br>'; /* for line break */
echo 'requested data : <pre>';
print_r($_POST);
?>
HTML JavaScript Code
File : post.html
<!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
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook