jQuery में $.get() method का use GET Type की request send करने के लिए किया जाता है। jQuery AJAX में server से communicate करने का यह सबसे आसान तरीका है।

Understanding Request : GET / POST

get() method use करने से पहले यह समझते हैं कि Request Type क्या होता है

Basically request दो तरह की होती है , GET & POST .

  1. GET : GET Request के साथ हमेशा Query String Form ('key = value ') में ही data send कर सकते हैं , जो कि URL में append होता है। इसे कोई भी देख सकता है। और limited data ही send कर सकते हैं। इसलिए mostly इसका use Data retrieve करने के लिए किया जाता है।

  2. POST : POST Request का use भी हम data retrieve करने के लिए कर सकते है ,  लेकिन POST Type के साथ submit किया गया data कोई देख नहीं पाता।

jQuery $.get() Syntax

$.get('/path/to/file', function(reponse, status) {
  /*optional stuff to do after success */
});

जिसमे callback function तब run होता है , जब request complete हो जाती है और response ready होता है। ये request response और status return करता है।

jQuery get() Example

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jQuery $.get() Method </title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <p class="target_elm">Click bellow buttons button to see result.</p> <p> <button class="send_request">Send Request</button> </p> <script type="text/javascript"> window.onload = function(){ /*check if jquery working*/ if(window.jQuery){ $(".send_request").click(function(event) { /*send request with query string data*/ $.get('handle_request.php?fname=Rahul&lname=Rajput', function(response, status, xhr) { /*set resturn data*/ $(".target_elm").html(response); /*see in console , what's coming from server*/ console.log('response', response); console.log('status', status); console.log('xhr', xhr); }); }); } } </script> </body> </html>

File : handle_request.php

<?php echo 'Full Name : '.$_GET['fname']. ' '. $_GET['lname']; ?>

ऊपर दिए गए example को run करने से पहले Browser Console open करले जिससे आप Output देख सकें।

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