AJAX Sending GET Request In Hindi

📔 : AJAX 🔗

पिछले topic में आपने पढ़ा कि AJAX Request send करने के लिए सबसे पहले JavaScript की predefined class XMLHttpRequest का Object बनाना पड़ता है। और उस Object की सभी possible properties और method के बारे में जाना।


इस topic में XMLHttpRequest का use करके एक GET Request send करेंगे।

AJAX GET Example

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

File : handle_request.php

Copy Fullscreen Close Fullscreen
<?php 
   echo 'GET Request Received.';
 ?>

Now , एक html file बनाते हैं , जिसमे AJAX Request send करने के लिए JavaScript Code लिखेंगे।

File : test.html

CopyFullscreenClose Fullscreen
<!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 करते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

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

b2eprogrammers