Not use JSON parser on response and a bit tidy-up

This commit is contained in:
Dita Aji Pratama 2024-02-11 13:20:10 +07:00
parent 7225d90e62
commit 4d150ebce9

View File

@ -1,33 +1,18 @@
function sendHttpRequest(url, method, data, callback, contentType = "multipart/form-data") { function sendHttpRequest(url, method, data, callback, contentType = "multipart/form-data") {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open(method, url, true); xhr.open(method, url, true);
xhr.setRequestHeader("Content-Type", contentType); xhr.setRequestHeader("Content-Type", contentType);
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (xhr.readyState === 4) { if (xhr.readyState === 4) {
if (xhr.status === 200) { if (xhr.status === 200) {
// Successful response var response = xhr.responseText;
var response;
if (contentType === "application/json") {
response = JSON.parse(xhr.responseText);
} else {
response = xhr.responseText;
}
callback(null, response); callback(null, response);
} else {
// Error response
callback(xhr.status, null);
} }
else callback(xhr.status, null);
} }
}; };
var requestData; var requestData;
if (contentType === "application/json") { if (contentType === "application/json") requestData = JSON.stringify(data);
requestData = JSON.stringify(data); else requestData = data;
} else {
requestData = data;
}
xhr.send(requestData); xhr.send(requestData);
} }