Skip to content

Commit 2ffcb66

Browse files
authored
Update utils.js
1 parent c6cb65c commit 2ffcb66

File tree

1 file changed

+51
-99
lines changed

1 file changed

+51
-99
lines changed

utils.js

Lines changed: 51 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -234,117 +234,69 @@ let copy = (text) => {
234234

235235

236236
// HTTP Request
237-
let axios = {
238-
239-
'get': (url, auth) => {
240-
241-
return new Promise((resolve, reject) => {
242-
243-
const xmlhttp = new XMLHttpRequest();
244-
245-
xmlhttp.onload = () => {
246-
resolve(JSON.parse(xmlhttp.responseText));
247-
};
248-
249-
xmlhttp.onerror = (e) => {
250-
console.log('error');
251-
reject(e);
252-
};
253237

254-
xmlhttp.open('GET', url, true);
255-
256-
xmlhttp.setRequestHeader('Accept', 'application/json');
257-
if (auth) xmlhttp.setRequestHeader('Authorization', 'token ' + auth);
258-
259-
xmlhttp.send();
260-
238+
let axios = {
239+
'get': (url, token) => {
240+
return new Promise((resolve, reject) => {
241+
try {
242+
var xmlhttp = new XMLHttpRequest();
243+
xmlhttp.onreadystatechange = function () {
244+
if (this.readyState == 4 && this.status == 200) {
245+
resolve(JSON.parse(this.responseText));
246+
}
247+
};
248+
xmlhttp.open('GET', url, true);
249+
if (token) xmlhttp.setRequestHeader('Authorization', 'token ' + token);
250+
xmlhttp.send();
251+
} catch(e) { reject(e) }
261252
});
262-
263253
},
264-
265-
'post': (url, data, auth) => {
266-
254+
'post': (url, data, token) => {
267255
return new Promise((resolve, reject) => {
268-
269-
const xmlhttp = new XMLHttpRequest();
270-
271-
xmlhttp.onreadystatechange = () => {
272-
if (xmlhttp.readyState == 4 && (xmlhttp.status == 201 || xmlhttp.status == 200)) {
273-
resolve(JSON.parse(xmlhttp.responseText));
274-
}
275-
};
276-
277-
xmlhttp.onerror = (e) => {
278-
reject(e);
279-
};
280-
281-
xmlhttp.open('POST', url, true);
282-
283-
xmlhttp.setRequestHeader('Accept', 'application/json');
284-
if (auth) xmlhttp.setRequestHeader('Authorization', 'token ' + auth);
285-
286-
data = data ? JSON.stringify(data) : '';
287-
288-
xmlhttp.send(data);
289-
256+
try {
257+
var xmlhttp = new XMLHttpRequest();
258+
xmlhttp.onreadystatechange = function () {
259+
if (this.readyState == 4 && (this.status == 201 || this.status == 200)) {
260+
resolve(JSON.parse(this.responseText));
261+
}
262+
};
263+
xmlhttp.open('POST', url, true);
264+
xmlhttp.setRequestHeader('Accept', 'application/json');
265+
xmlhttp.setRequestHeader('Authorization', 'token ' + token);
266+
xmlhttp.send(JSON.stringify(data));
267+
} catch(e) { reject(e) }
290268
});
291-
292269
},
293-
294-
'put': (url, data, auth) => {
295-
270+
'put': (url, token, data) => {
296271
return new Promise((resolve, reject) => {
297-
298-
const xmlhttp = new XMLHttpRequest();
299-
300-
xmlhttp.onreadystatechange = function() {
301-
if (this.readyState == 4 && (this.status == 201 || this.status == 200)) {
302-
resolve(JSON.parse(xmlhttp.responseText));
303-
}
304-
};
305-
306-
xmlhttp.onerror = (e) => {
307-
reject(e);
308-
};
309-
310-
xmlhttp.open('PUT', url, true);
311-
312-
xmlhttp.setRequestHeader('Accept', 'application/json');
313-
if (auth) xmlhttp.setRequestHeader('Authorization', 'token ' + auth);
314-
315-
data = data ? JSON.stringify(data) : '';
316-
317-
xmlhttp.send(data);
318-
272+
try {
273+
var xmlhttp = new XMLHttpRequest();
274+
xmlhttp.onreadystatechange = function () {
275+
if (this.readyState == 4 && (this.status == 201 || this.status == 200)) {
276+
resolve(JSON.parse(this.responseText));
277+
}
278+
};
279+
xmlhttp.open('PUT', url, true);
280+
xmlhttp.setRequestHeader('Authorization', 'token ' + token);
281+
xmlhttp.send(JSON.stringify(data));
282+
} catch(e) { reject(e) }
319283
});
320-
321284
},
322-
323-
'delete': (url, auth) => {
324-
285+
'delete': (url, token) => {
325286
return new Promise((resolve, reject) => {
326-
327-
const xmlhttp = new XMLHttpRequest();
328-
329-
xmlhttp.onload = () => {
330-
resolve(JSON.parse(xmlhttp.responseText));
331-
};
332-
333-
xmlhttp.onerror = (e) => {
334-
reject(e);
335-
};
336-
337-
xmlhttp.open('DELETE', url, true);
338-
339-
xmlhttp.setRequestHeader('Accept', 'application/json');
340-
if (auth) xmlhttp.setRequestHeader('Authorization', 'token ' + auth);
341-
342-
xmlhttp.send();
343-
287+
try {
288+
var xmlhttp = new XMLHttpRequest();
289+
xmlhttp.onreadystatechange = function () {
290+
if (this.readyState == 4 && this.status == 200) {
291+
resolve(JSON.parse(this.responseText));
292+
}
293+
};
294+
xmlhttp.open('DELETE', url, true);
295+
xmlhttp.setRequestHeader('Authorization', 'token ' + token);
296+
xmlhttp.send();
297+
} catch(e) { reject(e) }
344298
});
345-
346299
}
347-
348300
};
349301

350302

0 commit comments

Comments
 (0)