JavaScript
  Home arrow JavaScript arrow Page 2 - Programmatic POST Requests with JavaScript...
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

Programmatic POST Requests with JavaScript: A Functional Form Emulator
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2005-07-27

    Table of Contents:
  • Programmatic POST Requests with JavaScript: A Functional Form Emulator
  • Building a functional script: listing the “getXMLHTTPObject()” and “sendRequest()” functions
  • Getting the form’s (X)HTML markup: defining the “getFormCode()” function
  • Getting form data: defining the “getFormAction()” and “getFormVariables()” functions
  • Generating random data: defining the “getRandomValue()” and “getRandomEmail()” functions

  • 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


    Programmatic POST Requests with JavaScript: A Functional Form Emulator - Building a functional script: listing the “getXMLHTTPObject()” and “sendRequest()” functions


    (Page 2 of 5 )

    Throughout the previous part of the series, I covered in detail these functions, so I won’t stop long explaining what they do. Essentially, the first function is responsible for instantiating XmlHttpRequest objects each time it’s invoked, while the second one makes http requests to a given server. Retaking the program’s flow, they’re listed below:

    // function getXMLHTTPObject

    function getXMLHTTPObject(){

        //instantiate new XMLHttpRequest object

        var objhttp=(window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');

        if(!objhttp){return};

        // assign event handler

        objhttp.onreadystatechange=displayStatus;

        // return XMLHttpRequest object

        return objhttp;

    }

    // function sendRequest

    function sendRequest(url,data,method,mode,header){

        // set default values

        if(!url){url='default_url.htm'};

        if(!data){data='defaultdata=defaultvalue'};

        if(!method){method='post'};

        if(!mode){mode=true};

        if(!header){header='Content-Type:application/x-www-form-urlencoded; charset=UTF-8'};

        // get XMLHttpRequest object

        objhttp=getXMLHTTPObject();

        // open socket connection

        objhttp.open(method,url,mode);

        // set http header

        objhttp.setRequestHeader(header.split(':')[0],header.split(':')[1]);

        // send data

        objhttp.send(data);

    }

    As I said before, the above functions implement actually the core logic of the program, because they provide the required functionality to use cross-browser XmlHttpRequest objects, and send http requests to a specific host as well. However, I explained previously that the program was capable of performing additional tasks, such as getting data about the form to be emulated. Thus, over the next section, I’ll define the functions tasked with obtaining the form’s source code, as well as its action attribute along with its field names.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This third part of the series covers in detail each relevant function for building a...
       · Happily Internet Explorer raises a warning dialog when you try to open a stream from...
       · Thanks for the comments.Yes, warning dialogs are good and fine for well-intented...
     

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek