Fix: send request / receive json data
This commit is contained in:
+1
-2
@@ -33,5 +33,4 @@ function logout() {
|
||||
maestroID = -1;
|
||||
sessionStorage.setItem("maestroID", maestroID);
|
||||
location.href = './../main/index.html';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
function sendRequestServer(url, sendParams, onSuccess, onFail, isDebugMode) {
|
||||
// console.log(url);
|
||||
// console.log(sendParams);
|
||||
// console.log(onSuccess);
|
||||
// console.log(onFail);
|
||||
// console.log(isDebugMode);
|
||||
|
||||
let xhr = new XMLHttpRequest(); //new로 생성.
|
||||
xhr.open('POST', url, true);
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
xhr.send(sendParams);
|
||||
xhr.onload = function() {
|
||||
if(xhr.readyState === 4 && xhr.status === 200) {
|
||||
// console.log(xhr.responseText);
|
||||
// console.log(xhr.responseText.length);
|
||||
|
||||
if(xhr.responseText.length === 0 || xhr.responseText === null) {
|
||||
let error_message = "서버에서 요청을 수행하지 못했습니다.";
|
||||
printErrorMessage(error_message, onFail, isDebugMode);
|
||||
return;
|
||||
}
|
||||
|
||||
let jsonReply = JSON.parse(xhr.responseText);
|
||||
// console.log(jsonReply);
|
||||
|
||||
if(jsonReply === null) {
|
||||
let error_message = "서버에서 데이터가 넘어오지 않았습니다.";
|
||||
printErrorMessage(error_message, onFail, isDebugMode);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!isSuccess(jsonReply)) {
|
||||
let error_message = getErrorMessage(jsonReply);
|
||||
printErrorMessage(error_message, onFail, isDebugMode);
|
||||
return;
|
||||
}
|
||||
|
||||
onSuccess(jsonReply);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isSuccess(jsonReply) {
|
||||
if(jsonReply["result"] === undefined)
|
||||
return false;
|
||||
|
||||
if(jsonReply["result"] === "fail")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getErrorMessage(jsonReply) {
|
||||
return jsonReply["error"];
|
||||
}
|
||||
|
||||
function printErrorMessage(message, onFail, isDebugMode) {
|
||||
if(onFail !== undefined && onFail !== null)
|
||||
onFail(message);
|
||||
|
||||
if(isDebugMode === true)
|
||||
console.log(message);
|
||||
}
|
||||
Reference in New Issue
Block a user