This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.class.js
49 lines (49 loc) · 1.73 KB
/
connector.class.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
'use strict';
export default class Connector {
constructor() {
this.params = {
}
}
static getData(URL, callback){
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.response);
}
xmlHttp.open("GET", URL, true); // true for asynchronous
xmlHttp.send(null);
}
static postData(URL, data, success, parseType = null){
// console.log(URL);
let params = null;
if(parseType){
params = JSON.stringify(data);
}else{
params = typeof data == 'string' ? data : Object.keys(data).map(
function(k){ return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]); }
).join('&');
}
// console.log(params);
let xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open('POST', URL);
xmlHttp.onload = function() {
if (xmlHttp.readyState>3 && xmlHttp.status==200) {
// console.log('xmlHttp success');
success(xmlHttp.response);
}else{
console.log('xmlHttp error');
console.log('xmlHttp status: ' + xmlHttp.status);
}
};
xmlHttp.setRequestHeader('Access-Control-Allow-Origin', '*');
xmlHttp.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST, PUT');
xmlHttp.setRequestHeader('Access-Control-Allow-Headers', 'Content-Type');
xmlHttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.onerror = function (e) {
console.log("** An error occurred during the transaction: " + e);
};
xmlHttp.send(params);
return xmlHttp;
}
}