JavaScript
  Home arrow JavaScript arrow Page 2 - Ajax and the JQuery JavaScript Library
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Ajax and the JQuery JavaScript Library
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-09-23

    Table of Contents:
  • Ajax and the JQuery JavaScript Library
  • Triggering GET and POST HTTP requests with Ajax
  • Sending GET and POST requests with Ajax
  • Displaying and hiding elements in a web document

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Ajax and the JQuery JavaScript Library - Triggering GET and POST HTTP requests with Ajax


    (Page 2 of 4 )

    As I mentioned at the beginning, jQuery includes handy methods aimed specifically at working with Ajax in a simple fashion. The first of these Ajax-related methods is called “$.get().” Not surprisingly, it can be used for triggering GET HTTP requests to a web server.

    The following example demonstrates how to use this handy method to save my first and last names to a text file, which will be displayed back to the client via an alert box. Here it is:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Basic example on using jQuery with Ajax and $.get() method</title>

    <style type="text/css">

    body{

    padding: 0;

    margin: 0;

    background: #fff;

    }

    h1{

    font: 24px bold Arial, Helvetica, sans-serif;

    color:#000;

    }

    </style>

    <script type="text/javascript" src="jquery.js"></script>

    <script type="text/javascript">

    $(document).ready(function(){

    $.get("savedata.php", {fname: "Alejandro",lname: "Gervasio" },function(data){alert("The following data was saved: " + data);});

    });

    </script>

    </head>

    <body>

    <h1>Basic example on using jQuery with Ajax and get() method</h1>

    </body>

    </html>

    (definition of "savedata.php” file)

    <?php

    $data=$_GET['fname'].' '.$_GET['lname'];

    $fp=fopen('data.txt','w');

    fwrite($fp,$data);

    fclose($fp);

    echo $data;

    ?>

    As you can see, the previous code sample shows how to use the “get()” method to save some basic data to a destination text file via Ajax. This method also accepts a callback function as an incoming argument, which can be called to return data from the server.

    In this particular case, after saving my first and last names to a “data.txt” file, the script will display this information by using an alert box. Additionally, it’s possible to build another Ajax application that saves this sample data by way of a POST HTTP request. Of course, in this case, a “$.post()” method will be used instead, in the following manner:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Basic example on using jQuery with Ajax and $.post() method</title>

    <style type="text/css">

    body{

    padding: 0;

    margin: 0;

    background: #fff;

    }

    h1{

    font: 24px bold Arial, Helvetica, sans-serif;

    color:#000;

    }

    #samplelist{

    width: 30%;

    margin-left: auto;

    margin-right: auto;

    background: #eee;

    }

    .item{

    font: 11px Arial, Helvetica, sans-serif;

    color: #009;

    }

    </style>

    <script type="text/javascript" src="jquery.js"></script>

    <script type="text/javascript">

    $(document).ready(function(){

    $.post("savedata.php", {fname: "Alejandro",lname: "Gervasio" },function(data){alert("The following data was saved: " + data);});

    });

    </script>

    </head>

    <body>

    <h1>Basic example on using jQuery with Ajax and post() method</h1>

    </body>

    </html>

    (definition of "savedata.php” file)

    <?php

    $data=$_POST['fname'].' '.$_POST['lname'];

    $fp=fopen('data.txt','w');

    fwrite($fp,$data);

    fclose($fp);

    echo $data;

    ?>

    As you can see, the two previous hands-on examples demonstrate how simple it is to develop Ajax applications with the “jQuery” library, since the “$.get()” and “$.post()” methods shown before are fairly straightforward.

    Well, now that you hopefully learned how to create some simple Ajax programs, it’s time to examine yet another handy method that comes bundled with jQuery. This one is simply called “$.ajax()” and as its name would suggest, it triggers both GET and POST requests to a specified web server.

    However, to learn more about how to use this method, you’ll have to click on the link below and read the following section.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This installment of the series shows how to build some basic AJAX applications with...
       · This was an outstanding little lesson. If a student now tries adding a click event...
       · Good to know the article has been helpful for you.Thanks.
     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek