-
Notifications
You must be signed in to change notification settings - Fork 6
/
Ajax.js
232 lines (210 loc) · 6.31 KB
/
Ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Copyright (c) 2006, Opera Software ASA
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Opera Software ASA nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY OPERA SOFTWARE ASA AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL OPERA SOFTWARE ASA AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Check for the Opera namespace and if its not setup set it up.
* Also check for the Opera.Net namespace and if its not setup set it up.
*/
if(!Opera) var Opera = new Object();
if(!Opera.Net) Opera.Net = new Object();
/**
* The Opera.Net.Ajax class which handles the AJAX requests
* @constructor
*/
Opera.Net.Ajax = function() {
/** @private */
var self = this;
/** @private */
var http_request;
/** @private */
var timeout_interval = null; // By default there is no timeout
/**
* The Ajax Exception class
*
* @param {String} status
* @param {String} statusText
*/
function AjaxException(status, statusText) {
this.status = status;
this.statusText = statusText;
this.toString = function() {
return status + " " + statusText;
}
}
/**
* The NetworkError callback. This callback is called when there is a
* error on the server or the request times out.
*/
this.NetworkError = function(e) {
if(typeof opera != "undefined") if(typeof opera.postError != "undefined") opera.postError(e);
}
/**
* A private function which is called when the object
* is created.
*/
function Init() {
http_request = CreateHttpRequest();
}
/**
* A private function to create a HttpRequest on different browsers.
*/
function CreateHttpRequest() {
var req = null;
if(window.XMLHttpRequest) {
try {
req = new XMLHttpRequest();
}
catch(e) {
req = false;
}
}
else if(window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
req = false;
}
}
}
return req;
}
/**
* The timeout for a response. It should be more than 1 second.
*
* @param {Number} seconds
*/
this.setTimeout = function(seconds) {
if(seconds < 0) seconds = null;
timeout_interval = seconds;
}
/**
* The wrapper around the GET http method to get a
* file from the server
*
* @param {String} url
* @param {function} callback
*/
this.Get = function(url, callback) {
this.Connect("GET", url, "", callback);
}
/**
* The wrapper around the POST http method to get a
* file from the server
*
* @param {String} url
* @param {String} postdata
* @param {function} callback
*/
this.Post = function(url, postdata, callback) {
this.Connect("POST", url, postdata, callback);
}
/**
* A generic function to call a HTTP method on the server.
*
* The httpmethod can be GET, POST, PUT... and any method that
* HTTP supports.
*
* @param {String} httpmethod
* @param {String} url
* @param {String} senddata
* @param {function} callback
*/
this.Connect = function(httpmethod, url, senddata, callback) {
http_request.open(httpmethod, url, true);
http_request.onreadystatechange = function() {
if (http_request.readyState == 4) {
try {
if(self.NetworkError) {
/*
* http_request.statusCode will raise an exception if there
* is an error on the server. We also raise an error if the
* status code is not 200 or 0.
*/
if(http_request.status != 200 && http_request.status != 0) {
throw new AjaxException(http_request.status, http_request.statusText);
}
}
callback(http_request);
} catch(e) {
if(self.NetworkError) self.NetworkError(e);
}
}
}
if(httpmethod == "POST") {
http_request.setRequestHeader("Method", "POST " + url + " HTTP/1.1");
http_request.setRequestHeader('Content-Type', 'text/xml');
http_request.setRequestHeader('Content-Length', senddata.length);
}
try {
http_request.send(senddata);
} catch(e) {
if(self.NetworkError) self.NetworkError(new AjaxException(0, e.message));
}
}
/**
* To setup custom Request Header in the xmlhttp object
*
* @param {String} name
* @param {String} value
*/
this.setRequestHeader = function(name, value) {
http_request.setRequestHeader(name, value);
}
/**
* To abort a XmlHttp request.
*/
this.abort = function() {
http_request.abort();
}
/**
* Returns the value of the named response header.
*
* @param {String} name
*/
this.getResponseHeader = function (name) {
return http_request.getResponseHeader(name);
}
/**
* Returns a string containg all the response headers.
*/
this.getAllResponseHeaders = function () {
return http_request.getResponseHeader(name);
}
/**
* A property to get the XmlHttp object associated with this class.
*/
this.getXmlHttp = function() {
return http_request;
}
Init();
if(http_request) return this;
else return null;
}