From b5b56e8c2dba0ccf45d758d1ebb4074eab78a6ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=A2=E1=84=91=E1=85=B3=E1=86=AF=20=E1=84=82?= =?UTF-8?q?=E1=85=A9=E1=84=90=E1=85=B3=E1=84=87=E1=85=AE=E1=86=A8?= Date: Tue, 10 Jul 2018 18:03:46 +0900 Subject: [PATCH] Fix: send request / receive json data --- src/web/admin/admin_register_list.html | 40 ++++++++++------ src/web/admin/login.html | 54 +++++++++++----------- src/web/js/main.js | 3 +- src/web/js/request_server.js | 63 ++++++++++++++++++++++++++ src/web/server/lib/send_error_code.php | 10 ---- src/web/server/lib/send_reply_json.php | 35 ++++++++++++++ src/web/server/maestro/login.php | 30 ++++++------ src/web/server/setup/connect_db.php | 27 ++++------- 8 files changed, 175 insertions(+), 87 deletions(-) create mode 100644 src/web/js/request_server.js delete mode 100644 src/web/server/lib/send_error_code.php create mode 100644 src/web/server/lib/send_reply_json.php diff --git a/src/web/admin/admin_register_list.html b/src/web/admin/admin_register_list.html index ff3ccdf..08aa4c6 100644 --- a/src/web/admin/admin_register_list.html +++ b/src/web/admin/admin_register_list.html @@ -25,11 +25,11 @@ let maestroName = $("#search_name").val(); if(maestroName.length === 0) { - // $("#search_name_notice").val("학생 이름을 입력하세요."); + $("#error_message").text("학생 이름을 입력하세요."); $("#search_name").focus(); } - loadMaestroList($maestroName); + loadMaestroList(maestroName); } function loadMaestroList($maestroName) { @@ -43,6 +43,7 @@ // console.log(xhr.responseText.length); if(xhr.responseText.length === 0 || xhr.responseText === null) { console.log("no data from server"); + $("#error_message").text("서버에서 요청을 수행하지 못했습니다."); return; } @@ -51,16 +52,19 @@ if(replyJSON === null) { console.log("no data from server"); + $("#error_message").text("서버에서 데이터가 넘어오지 않았습니다."); return; } if(replyJSON["ERROR"] !== undefined || replyJSON["RESULT"] === undefined) { console.log(replyJSON["ERROR"]); + $("#error_message").text(replyJSON["ERROR"]); return; } if(replyJSON["RESULT"] === "fail") { console.log(replyJSON["RESULT"]); + $("#error_message").text(replyJSON["ERROR"]); return; } @@ -138,6 +142,7 @@ // console.log(xhr.responseText.length); if(xhr.responseText.length === 0 || xhr.responseText === null) { console.log("no data from server"); + $("#error_message").text("서버에서 요청을 수행하지 못했습니다."); return; } @@ -146,20 +151,24 @@ if(replyJSON === null) { console.log("no data from server"); + $("#error_message").text("서버에서 데이터가 넘어오지 않았습니다."); return; } if(replyJSON["ERROR"] !== undefined || replyJSON["RESULT"] === undefined) { console.log(replyJSON["ERROR"]); + $("#error_message").text(replyJSON["ERROR"]); return; } if(replyJSON["RESULT"] === "fail") { console.log(replyJSON["RESULT"]); + $("#error_message").text(replyJSON["ERROR"]); return; } loadMaestroList(""); + $("#error_message").text("마에스트로 계정이 정상적으로 등록되었습니다."); } } } @@ -177,19 +186,24 @@
- -

검색

+
+
- 마에스트로 이름 -
-
-
+
+ +

검색

- - 검색 결과 -
-
-
+ 마에스트로 이름 +
+
+
+ + + 검색 결과 +
+
+
+
diff --git a/src/web/admin/login.html b/src/web/admin/login.html index f833daf..5562e99 100644 --- a/src/web/admin/login.html +++ b/src/web/admin/login.html @@ -10,6 +10,7 @@ + @@ -54,17 +50,19 @@ -
- 관리자 아이디 -
- 암호 -
-
+
+ 관리자 아이디 +
+ 암호 +
+
-
- 로그인 -
+
+ 로그인 +
+
+
diff --git a/src/web/js/main.js b/src/web/js/main.js index 530790c..fd8ee27 100644 --- a/src/web/js/main.js +++ b/src/web/js/main.js @@ -33,5 +33,4 @@ function logout() { maestroID = -1; sessionStorage.setItem("maestroID", maestroID); location.href = './../main/index.html'; -} - +} \ No newline at end of file diff --git a/src/web/js/request_server.js b/src/web/js/request_server.js new file mode 100644 index 0000000..21cb1e8 --- /dev/null +++ b/src/web/js/request_server.js @@ -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); +} \ No newline at end of file diff --git a/src/web/server/lib/send_error_code.php b/src/web/server/lib/send_error_code.php deleted file mode 100644 index d3eeb28..0000000 --- a/src/web/server/lib/send_error_code.php +++ /dev/null @@ -1,10 +0,0 @@ - $error_code - ); - echo json_encode($returnValue, JSON_UNESCAPED_UNICODE); -} - -?> \ No newline at end of file diff --git a/src/web/server/lib/send_reply_json.php b/src/web/server/lib/send_reply_json.php new file mode 100644 index 0000000..a0339bc --- /dev/null +++ b/src/web/server/lib/send_reply_json.php @@ -0,0 +1,35 @@ +close(); +} + +function send_result_fail() { + global $jsonReply; + global $db_conn; + + $jsonReply["result"] = "fail"; + echo json_encode($jsonReply, JSON_UNESCAPED_UNICODE); + $db_conn->close(); +} + +?> \ No newline at end of file diff --git a/src/web/server/maestro/login.php b/src/web/server/maestro/login.php index e3a4995..786f919 100644 --- a/src/web/server/maestro/login.php +++ b/src/web/server/maestro/login.php @@ -1,9 +1,11 @@ close(); + set_error_message("등록되지 않은 마에스트로 아이디입니다."); + send_result_fail(); exit; } -$replyJSON = login($maestro_name, $password); -if($replyJSON === null || $replyJSON[MaestroID] === null) { - $replyJSON["ERROR"] = "비밀번호를 잘못 입력하셨습니다."; - echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE); - $db_conn->close(); +$login_data = login($maestro_name, $password); +if($login_data === null || $login_data[maestroID] === null) { + set_error_message("비밀번호가 틀렸습니다."); + send_result_fail(); exit; } -echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE); -$db_conn->close(); +set_data("maestroId", $login_data[maestroID]); +send_result_success(); exit; + + function get_maestro_id($maestro_name) { global $db_conn; @@ -65,8 +65,8 @@ function login($maestro_name, $password) { $stmt->fetch(); $stmt->close(); - $replyJSON["MaestroID"] = $maestro_id; - return $replyJSON; + $result["maestroID"] = $maestro_id; + return $result; } ?> \ No newline at end of file diff --git a/src/web/server/setup/connect_db.php b/src/web/server/setup/connect_db.php index ab1374d..fd45821 100644 --- a/src/web/server/setup/connect_db.php +++ b/src/web/server/setup/connect_db.php @@ -1,7 +1,7 @@ connect_error) { - send_error_code("Connection failed: ".$db_conn->connect_error); + set_error_message("Failed to connect DB: ".$db_conn->connect_error); + send_result_fail(); exit; -} - -$query = "USE jisangs"; -$result = mysqli_query($db_conn, $query); - - -function send_error_message($replyJSON, $error_message) { - $replyJSON["ERROR"] = $error_message; - echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE); - // $db_conn->close(); -} - -function send_result_message($replyJSON, $result_message) { - $replyJSON["RESULT"] = $result_message; - echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE); - // $db_conn->close(); +} else { + $query = "USE jisangs"; + $result = mysqli_query($db_conn, $query); } ?> \ No newline at end of file