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 में आपने पढ़ा कि AJAX Request send करने के लिए सबसे पहले JavaScript की predefined class XMLHttpRequest का Object बनाना पड़ता है। और उस Object की सभी possible properties और method के बारे में जाना।
इस topic में XMLHttpRequest का use करके एक GET Request send करेंगे।
सबसे पहले एक PHP File बनाते हैं , जिसमे उस request को handle कर सके।
File : handle_request.php
<?php
echo 'GET Request Received.';
?>
Now , एक html file बनाते हैं , जिसमे AJAX Request send करने के लिए JavaScript Code लिखेंगे।
File : test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AJAX GET Request</title>
</head>
<body>
<div id="result">Here result will be show.</div> <br>
<button onclick="send_get_request()">Click Here</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").innerText = this.responseText;
}
};
xhttpreq.open("GET", "handle_request.php", true);
xhttpreq.send();
}
</script>
</body>
</html>
❕ Important
Note , Request handle करने के लिए किसी server-side language और उसे Run करने के लिए Environment की जरूरत होती होती है , Example में PHP use की गयी है , और उसे run करने के लिए XAMPP Control Panel use किया गया है।
Learn PHP Tutorials Now
चूंकि GET Request में हमेशा query string की form में ही data send किया जाता है , जिसमे key=value pair में data रखते हैं।
For Example :
xhttpreq.open("GET", "handle_request.php?name=Rahul Kumar&age=24&about=Developer", true);
I Hope , आपको समझ में आ गया होगा कि , JavaScript में किस तरह से GET Request Send करते हैं।
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