]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/util/RemoteRequest.js
get the xmlhttp host dynamically.
[working/Evergreen.git] / Open-ILS / src / javascript / util / RemoteRequest.js
1 /* dynamically determine the XMLHTTPRequest server based on 
2         where the client is coming from.  Good for SSH tunneling, etc. */
3 var _url = "https://" + globalRootURL;
4 if(globalPort != "443")
5         _url = _url + ":" + globalPort;
6 var XML_HTTP_URL = _url + "/gateway";
7
8
9 /* Request object */
10 function RemoteRequest( service, method ) {
11
12         this.service    = service;
13         this.method             = method;
14         this.xmlhttp    = false;
15         this.name               = null;
16
17         /* give us the ability to ignore responses from cancelled searches */
18         this.cancelled = false; 
19
20         this.type               = "POST"; /* default */
21
22         var i = 2;
23         this.params = ""; 
24         while(arguments[i] != null) {
25                 var object = js2JSON(arguments[i++]);
26                 this.params += "&__param=" + encodeURIComponent(object);
27         }
28
29         if(!this.params) { this.params = ""; }
30         this.param_string = "service=" + service + "&method=" + method + this.params;
31         this.url = XML_HTTP_URL;
32
33         if( ! this.type || ! this.service || ! this.method ) {
34                 alert( "ERROR IN REQUEST PARAMS");
35                 return null;
36         }
37
38         /* try MS */
39         try { 
40                 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
41         } catch (e) {
42                 try { 
43                         this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
44                 } catch (E) {
45                         this.xmlhttp = false;
46                 }
47         }
48
49         if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
50                 this.xmlhttp = new XMLHttpRequest();
51         }
52
53         if(!this.xmlhttp) {
54                 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
55                 return null;
56         }
57
58 }
59
60 /* define the callback we use when this request has received
61         all of its data */
62 RemoteRequest.prototype.setCompleteCallback = function(callback) {
63         var object = this;
64         var obj = this.xmlhttp;
65         this.callback = callback;
66         this.xmlhttp.onreadystatechange = function() {
67                 if( obj.readyState == 4 ) {
68                         if(!this.cancelled)
69                                 callback(object);
70                 }
71         }
72 }
73
74 /** Send the request 
75   * By default, all calls are asynchronous.  if 'blocking' is
76   * set to true, then the call will block until a response
77   * is received.  If blocking, callbacks will not be called.
78   * In other, you can assume the data is avaiable as soon as the
79   * send call returns. 
80   */
81 RemoteRequest.prototype.send = function(blocking) {
82
83         var url = this.url;
84         var data = null;
85
86         if( this.type == 'GET' ) { 
87                 url +=  "?" + this.param_string; 
88         }
89
90         if(blocking) {
91                 this.xmlhttp.open(this.type, url, false);
92         } else {
93                 this.xmlhttp.open(this.type, url, true);
94         }
95
96
97         if( this.type == 'POST' ) {
98                 data = this.param_string;
99                 this.xmlhttp.setRequestHeader('Content-Type',
100                                 'application/x-www-form-urlencoded');
101         }
102
103         if(!this.cancelled)
104                 this.xmlhttp.send( data );
105 }
106
107 RemoteRequest.prototype.getText = function() {
108         return this.xmlhttp.responseText;
109 }
110
111 RemoteRequest.prototype.getResultObject = function() {
112         var obj = JSON2js( this.xmlhttp.responseText );
113
114         if(obj == null) {
115                 debug("received null response");
116                 return null;
117         }
118
119         if(obj && obj.is_err) { 
120                 debug("Something's Wrong: " + js2JSON(obj));
121                 throw new EXCommunication(obj.err_msg); 
122         }
123
124         if( obj[0] != null && obj[1] == null ) 
125                 return obj[0];
126
127         return obj;
128 }
129
130 RemoteRequest.prototype.addParam = function(param) {
131         this.param_string += "&__param=" + escape(js2JSON(param));
132 }