Basic Ajax usage with jQuery

In this article we will be talking about the basic usage of Ajax with jQuery 1.4.2 production (24KB, Minified and Gzipped).
In order to use the JavaScript examples in this article, you should first download jQuery and include it on your page using:
  1. <script type="text/javascript" src="jquery-1.4.2.js"></script>  
Throughout this article our Ajax scripts will communicate with ‘serverscript.php‘ our Server script.
  1. <?php  
  2. if(isset($_POST['firstname']) &&  
  3.    isset($_POST['lastname'])) {  
  4.  echo "Hey {$_POST['firstname']} {$_POST['lastname']}, 
  5.    you rock!\n(repsonse to your POST request)";  
  6. }  
  7. if(isset($_GET['firstname']) &&  
  8.    isset($_GET['lastname'])) {  
  9.  echo "Hey {$_GET['firstname']} {$_GET['lastname']}, 
  10.    you rock!\(response to your GET request)";  
  11. }  
  12. ?>  
Method One - POST (Asynchronous with data):
Transfer data to the server (using POST method), and retrieve a response:
  1. function doAjaxPost() {  
  2.  $.ajax({  
  3.    type: "POST",  
  4.    url: "serverscript.php",  
  5.    data: "firstname=clint&lastname=eastwood",  
  6.    success: function(resp){  
  7.      // we have the response  
  8.      alert("Server said:\n '" + resp + "'");  
  9.    },  
  10.    error: function(e){  
  11.      alert('Error: ' + e);  
  12.    }  
  13.  });  
  14. }  
  15.   
  16. doAjaxPost();  
Method Two - GET (Asynchronous with data):
Transfer data to the server (using GET method), and retreive a response:
  1. function doAjaxGet() {  
  2.  $.ajax({  
  3.    type: "GET",  
  4.    url: "serverscript.php",  
  5.    data: "firstname=clint&lastname=eastwood",  
  6.    success: function(resp){  
  7.      // we have the response  
  8.      alert("Server said:\n '" + resp + "'");  
  9.    },  
  10.    error: function(e){  
  11.      alert('Error: ' + e);  
  12.    }  
  13.  });  
  14. }  
  15.   
  16. doAjaxGet();  
Note that GET is the default type for Ajax calls using jQuery, so we really do not need to explicitly state it, but I’ve placed it there just for clarity.
Practical jQuery Example using POST:
  1. <html>  
  2. <head>  
  3.   
  4. <script type="text/javascript" src="jquery-1.4.2.js"></script>  
  5. </head>  
  6. <body>  
  7.   <script>  
  8. function doAjaxPost() {  
  9.  // get the form values  
  10.  var field_a = $('#field_a').val();  
  11.  var field_b = $('#field_b').val();  
  12.   
  13.  $.ajax({  
  14.    type: "POST",  
  15.    url: "serverscript.php",  
  16.    data: "firstname="+field_a+"&lastname="+field_b,  
  17.    success: function(resp){  
  18.      // we have the response  
  19.      alert("Server said:\n '" + resp + "'");  
  20.    },  
  21.    error: function(e){  
  22.      alert('Error: ' + e);  
  23.    }  
  24.  });  
  25. }  
  26. </script>  
  27.   
  28. First Name:  
  29. <input type="text" id="field_a" value="Sergio">  
  30. Last Name:  
  31. <input type="text" id="field_b" value="Leone">  
  32. <input type="button" value="Ajax Request" onClick="doAjaxPost()">  
  33.   
  34. </body>  
  35. </html>  


jQuery Example in Browser
Well there you have it, not too tricky and with a weight of only 24Kb for the base library you can’t go wrong. Of course, jQuery can be used for a whole heap of other tasks.
Until next time (when we cover MooTools), Happy A’jaxing!

Comments