-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpRequest.js
115 lines (113 loc) · 2.04 KB
/
HttpRequest.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
//https://developer.mozilla.org/pt-BR/docs/Web/API/XMLHTTPRequest
//https://developer.mozilla.org/pt-BR/docs/Web/API/XMLHttpRequest/Usando_XMLHttpRequest
//https://www.w3schools.com/js/js_ajax_http.asp
class AjaxRequest {
constructor() {
if(window.XMLHttpRequest)
{
try
{
this.request = new XMLHttpRequest();
}
catch(e)
{
throw new Error(e);
}
}
else
{
if(window.ActiveXObject)
{
try
{
this.request = new ActiveXObject("Xsxml2.XMLHTTP");
}
catch(e1)
{
try
{
this.request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e2)
{
throw new Error(e1+"\n"+e2);
}
}
}
else
{
if(window.createRequest)
{
try
{
this.request = window.createRequest();
}
catch(e)
{
throw new Error(e);
}
}
}
}
/*
* If we didn't succeed in making the request object, alert
* the caller of the problem.
*/
if(!this.request)
{
throw new Error("Couldn't create an XMLHttpRequest\n");
}
}
abort(){
this.request.abort();
}
getAllResponseHeaders(){
this.request.getAllResponseHeaders();
}
overrideMimeType(mime){
this.request.overrideMimeType(mime);
}
setRequestHeader(header, value){
this.request.setRequestHeader(header, value);
}
}
function JSONasyncRequest(url) {
var that = this;
var is_finished = false;
var status = 0;
var HttpReq = new AjaxRequest();
this.result = {};
this.url = (url)?url:"";
this.onfinished = function(req,parent){};
this.isfinished = function(){
return is_finished;
};
this.status = function(){
return status;
};
HttpReq.request.onreadystatechange = function() {
if(this.readyState!==this.DONE) {
return;
}
else {
is_finished = true;
}
status = this.status
if(this.status==200) {
try
{
that.result = JSON.parse(this.responseText);
that.onfinished(this,that);
}
catch(e)
{
throw new Error(e);
return;
}
}
};
this.get = function(){
HttpReq.request.open("GET", this.url, true);
HttpReq.request.send();
}
}