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