JavaScript
  Home arrow JavaScript arrow Page 3 - 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 
Sun Developer Network 
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: Automated Form Submissions
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 23
    2005-07-20

    Table of Contents:
  • Programmatic POST Requests with JavaScript: Automated Form Submissions
  • Automated POST requests: the basics of a JavaScript-based form emulator
  • Building the form emulator: defining the core functions “getXMLHTTPObject()” and “sendRequest()”
  • Tracking the request’s status: defining the “displayStatus()” function

  • 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: Automated Form Submissions - Building the form emulator: defining the core functions “getXMLHTTPObject()” and “sendRequest()”


    (Page 3 of 4 )

    To get started writing the form emulator, I’ll begin by defining the source code for the first functions, in conjunction with the proper explanation on the operations assigned within the JavaScript program. In first place, here is the definition for the “getXMLHTTPObject()” function:

    // 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;

    }

    As I said before, this function should be already familiar, because it was defined in the first part of the series. Essentially, what it does is to return a new instance of the XMLHttpRequest object each time it’s invoked, in this way encapsulating the code responsible for object instantiation. If you have some knowledge about design patterns, then it should be clear that the factory pattern is basically applied here. By using this function, I’m decoupling the process to create objects from the rest of the program code.

    The next function to be defined is “sendRequest()”, which acts as the workhorse of the program, since it makes all of the http requests. The structure of this function is essentially the same of that reviewed on my previous article, so shouldn’t be hard to understand. The only change that I’ve introduced within its code is the value assigned to the default http header. Due the fact that the script is aimed to emulate post forms, the http header specified is the following:

    'Content-Type:application/x-www-form-urlencoded; charset=UTF-8'

    The above header is the default value assigned on (X)HTML documents that implement regular forms. As long as a web server receives this header, it assumes that the request is coming from a web form. If the request method is set to POST, then the data is sent apart from the requested URL. Otherwise, if a GET method is used, the form data (name/value pairs) is appended to the URL as part of the query string.

    I suppose all of these concepts are closely familiar to you, thus having briefly explained the meaning of this header within the context of the HTTP protocol, here is code for the ” sendRequest()” function:

    // 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);

    }

    The only points worthy of mention on the above function are the default values assigned to the “Content-Type” http header, as well as the request mode. Essentially, the first request will be made in synchronous mode, because the program needs to get a response from the server, and then the subsequent ones will be performed asynchronously.

    The next thing to do is reviewing another relevant function within the form emulator program. Let’s have a look at the “displayStatus()” function, which shows descriptive information about the http request’s progress. Its definition is only a few lines away.

    More JavaScript Articles
    More By Alejandro Gervasio


       · The second part of the series explains the basics of POST http requests, used widely...
       · Hi,Based on your article, I should be able to retrieve the values sent with the...
       · Hello,Thanks for the comments.About your question, I've not tested...
       · Excellent serie,But, when i try with any compiled page get following error “Acceso...
       · Thank you for the kind comments on my Ajax article. Actually, it’s normal to get...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT