-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsAjax.js
397 lines (338 loc) · 12.7 KB
/
clsAjax.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
class clsAjax
{
constructor(options)
{
if(!options)
{
console.error('options must be defined');
}
this.options = options;
this.successArr = [];
this.errorArr = [];
if(!this.options.url)
{
console.error('options.url must be defined');
}
//Data Coming Back
if(typeof this.options.responseType === 'undefined')
{
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType
this.options.responseType = 'json'; //text (string), json, document (XML or HTML), arraybuffer (ArrayBuffer binary data), blob (object of binary data)
}
this.options.responseType = this.options.responseType.toLowerCase();
//Data Going Up (not standard and may not be needed) json, formdata, file upload?
if(typeof this.options.requestType === 'undefined')
{
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType
this.options.requestType = 'json'; //text (string), json, document (XML or HTML), arraybuffer (ArrayBuffer binary data), blob (object of binary data)
}
this.options.requestType = this.options.requestType.toLowerCase();
if(typeof this.options.auth === 'undefined')
{
this.options.auth = 'none'; //none, jwt, username/password
}
this.options.auth = this.options.auth.toLowerCase();
if(typeof this.options.async === 'undefined')
{
//XMLHttpRequest.open(method, url, true/false)
this.options.async = true;
}
if(typeof this.options.method === 'undefined')
{
//XMLHttpRequest.open(method, url, true/false)
this.options.method = 'get';
}
this.options.method = this.options.method.toLowerCase();
if(typeof this.options.success === 'undefined')
{
console.error('error: success must be defined')
}
else
{
this.addOnSuccess(this.options.success);
}
if(typeof this.options.error !== 'undefined')
{
this.addOnError(this.options.error);
}
this.request = new XMLHttpRequest();
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
//withCredentials --> this.options.auth
//The XMLHttpRequest.withCredentials property is a Boolean that indicates whether or not cross-site Access-Control requests should be made
//using credentials such as cookies, authorization headers or TLS client certificates.
//Setting withCredentials has no effect on same-site requests.
this.addEvents();
this.init();
}
init()
{
//XMLHttpRequest.open(method, url)
//XMLHttpRequest.open(method, url, async)
//XMLHttpRequest.open(method, url, async, user, password)
this.request.open(this.options.method, this.options.url, this.options.async);
//File Attachments and Form Data
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader
//The XMLHttpRequest method setRequestHeader() sets the value of an HTTP request header.
//When using setRequestHeader(), you must call it after calling open(), but before calling send().
//If this method is called several times with the same header, the values are merged into one single request header.
//request.setRequestHeader() Can be called multiple times, each time it is called it will add a new header. Therefore what you have done in your question is correct.
//Both of the following will be set
//this.request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
//this.request.setRequestHeader('Content-Type', 'text/html');
//JWT
//this.options.auth = jwt
//this.request.setRequestHeader('Authorization', localStorage.getItem('token'));
//If not exist then T.jwt.requestToken();
//this.request.setRequestHeader("x-filename", photoId);
//this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); //JSON
//this.request.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); //JSON in TEAM
/*
//POST
if (this.options.data instanceof FormData)
{
request.send(this.options.data);
}
else
{
var blob = new Blob([JSON.stringify(this.options.data)], { type: 'application/json' });
request.send(blob);
}
*/
//var data = new FormData();
//data.append('user', 'person');
//data.append('pwd', 'password');
//data.append('organization', 'place');
//data.append('requiredkey', 'key');
this.request.send(null);
//this.request.send(data);
}
addEvents()
{
this.request.addEventListener('loadstart', (e) =>
{
this.onLoadStart(e);
});
this.request.addEventListener('load', (e) =>
{
this.onLoad(e);
});
this.request.addEventListener('loadend', (e) =>
{
this.onLoadEnd(e);
});
this.request.addEventListener('progress', (e) =>
{
this.onProgress(e);
});
this.request.addEventListener('error', (e) =>
{
this.onError(e);
});
this.request.addEventListener('abort', (e) =>
{
this.onAbort(e);
});
this.request.addEventListener('timeout', (e) =>
{
this.onTimeout(e);
});
this.request.addEventListener('readystatechange', (e) =>
{
this.onReadyStateChange(e);
});
}
abort()
{
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/abort_event
//The abort event is fired when a request has been aborted, for example because the program called XMLHttpRequest.abort()
this.request.abort();
}
addOnSuccess(successFunc)
{
this.successArr.push(successFunc);
}
success(e)
{
for(let i = 0; i < this.successArr.length; i++)
{
//0 UNSENT, 1 OPENED, 3 LOADING OK, 4 DONE OK
//this.request.statusText
//this.successArr[i](this.request.responseText);
this.successArr[i](e.currentTarget.responseText);
}
}
addOnError(errorFunc)
{
this.errorArr.push(errorFunc);
}
error(e)
{
for(let i = 0; i < this.errorArr.length; i++)
{
//0 UNSENT, 1 OPENED, 3 LOADING OK, 4 DONE OK
//this.request.statusText
this.errorArr[i](e);
}
}
onReadyStateChange(e)
{
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange
//An EventHandler that is called whenever the readyState attribute changes.
//The callback is called from the user interface thread.
//The XMLHttpRequest.onreadystatechange property contains the event handler to be called when the readystatechange event is fired, that is every time the readyState property of the XMLHttpRequest changes.
//console.log('onReadyStateChange', e, this.request.status);
}
onLoadStart(e)
{
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/loadstart_event
//The loadstart event is fired when a request has started to load data.
//console.log('onLoadStart', e, this.request.status);
}
onLoad(e)
{
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/load_event
//The load event is fired when an XMLHttpRequest transaction completes successfully.
//console.log('onLoad', e, this.request.status);
if (this.request.readyState === this.request.DONE || this.request.readyState === 4)
{
if (this.request.status === 0 || this.request.status === 200 || this.request.status < 400)
{
//console.log(this.request.response);
//var data = JSON.parse(this.request.response);
this.success(e);
}
else if(this.request.status === 401)
{
//Unauthorized
//If using auth then something was wrong
//If using jwt then get new token
}
else
{
//console.error('There was an issue with the request');
this.error(e);
}
}
}
onLoadEnd(e)
{
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/loadend_event
//The loadend event is fired when a request has completed, whether successfully (after load) or unsuccessfully (after abort or error).
//console.log('onLoadEnd', e, this.request.status);
}
onTimeout(e)
{
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/timeout_event
//The timeout event is fired when progression is terminated due to preset time expiring.
//console.log('onTimeout', e, this.request.status);
this.error(e);
}
onProgress(e)
{
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/progress_event
//The progress event is fired periodically when a request receives more data.
let total = e.total;
let loaded = e.loaded;
let percent = 0;
if(total > 0 && loaded > 0)
{
percent = e.loaded / e.total * 100;
}
console.log(this.options.url, percent + '% :' + loaded + ' bytes transferred of ' + total + ' bytes\n');
}
onError(e)
{
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/error_event
//The error event is fired when the request encountered an error.
//console.log('onError', e, this.request.status);
this.error(e);
}
onAbort(e)
{
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/abort_event
//The abort event is fired when a request has been aborted, for example because the program called XMLHttpRequest.abort().
//console.log('onAbort', e, this.request.status);
}
}
/* Usage
let templateHtml;
let url = 'template.html';
let ajaxVar = new clsAjax(
{
url: url,
method: 'GET',
requestType: 'text',
responseType: 'text',
async: true,
auth: 'None',
data: {},
success: function(response)
{
//Do stuff
templateHtml = response; //If not Async
mySuccessFunction(templateHtml); //If Async
},
error: function(response)
{
console.log('error', response);
}
});
//Async Function
function mySuccessFunction(templateHtml)
{
console.log(templateHtml);
}
*/
//File Uploads
/*
let formData = new FormData();
let files = this.containerElement.querySelector('.fileUpload').files;
formData.append("clsFile", JSON.stringify(this.clsFile));
for (var i = 0; i < files.length; i++)
{
formData.append('file', files[i], files[i].name);
}
request.send(formData);
*/
/* Async methods
class HttpClient {
constructor(){}
async get(url) {
return new Promise( (resolve, reject) => {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = (evt) => {
if (evt.currentTarget.readyState === 4 && evt.currentTarget.status === 200) {
try {
const response = JSON.parse(evt.currentTarget.response);
resolve(response);
} catch (exception) {
reject(exception);
}
}
};
xhttp.open('GET', url, true);
xhttp.send();
});
}
async post(url, data) {
return new Promise( (resolve, reject) => {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = (evt) => {
if (evt.currentTarget.readyState === 4 && evt.currentTarget.status === 200) {
try {
const response = JSON.parse(evt.currentTarget.response);
resolve(response);
} catch (exception) {
reject(exception);
}
}
};
xhttp.open('POST', url, true);
xhttp.send(data);
});
}
}
const httpClient = new HttpClient();
const data = await httpClient.get('some url');
*/