easier direct editing of list items
[sgn.git] / js / CXGN / Request.js
blob7c1d0106c56d3bc17e112cc37ec22a093d1cf52a
1 /** 
3 =head1 NAME
5 Class: Request 
7 =head1 SYNOPSIS
9 A simple handler for Async Requests
11 The returned XML file should contain all information needed to cause a page effect,
12 rather than creating a custom response for each request, which is not possible due
13 to the security issues that prevent the creation of instances of XMLHttpRequest() 
14 with unique properties.  It's better this way, anyhow :-/
16 See XMLRoute.js to write handlers for async responses. 
18 =head1 USAGE
20 var req = new Request();
21 if(req.isValid()){
22         req.send("scraps/add.pl", "?first=3&second=2", "POST");
25 =head1 AUTHOR
27 Chris Carpita <csc32@cornell.edu>
29 =cut
33 JSAN.use("CXGN.Base");
34 JSAN.use("CXGN.XMLroute");
36 function Request() {
37         
38         var req = null;
39         this.valid = false;
40         var loader = this;
41                 
42         this.isValid = function() { return this.valid }
43         this._onReadyState = function() {
44                 if(req.readyState==4){
45 //                      document.write(req.responseText);
46                         XMLroute.call(this, req.responseXML);   
47                 }
48         }       
49         this.send = function(url, parameters, method) {
50                 if(!method) { method = "POST" }
51                 if(method == "POST") {
52                         req.open("POST", url, true);
53                         req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
54                 req.setRequestHeader("Content-length", parameters.length);
55                 req.setRequestHeader("Connection", "close");
56                         req.send(parameters);
57                 }
58                 else if(method == "GET") {
59                         req.open("GET", url + parameters, true);
60                         req.send(null);
61                 }
62         }
63         if(window.XMLHttpRequest) {
64                 req = new XMLHttpRequest();
65         }
66         else if (window.ActiveXObject) {
67                 try { req = new ActiveXObject("Msxml2.XMLHTTP");}
68                 catch(e) {
69                         try { req = new ActiveXObject ("Microsoft.XMLHTTP");}
70                         catch (e) { return false }
71                 }
72         }
73         if(req) { 
74                 this.valid = true;
75                 req.onreadystatechange = function() { loader._onReadyState.call() }
76                 return true;
77         }
78         else { return false }