Fix: send request / receive json data

This commit is contained in:
2018-07-10 18:03:46 +09:00
parent 7fe64c7a45
commit b5b56e8c2d
8 changed files with 175 additions and 87 deletions
+27 -13
View File
@@ -25,11 +25,11 @@
let maestroName = $("#search_name").val(); let maestroName = $("#search_name").val();
if(maestroName.length === 0) { if(maestroName.length === 0) {
// $("#search_name_notice").val("학생 이름을 입력하세요."); $("#error_message").text("학생 이름을 입력하세요.");
$("#search_name").focus(); $("#search_name").focus();
} }
loadMaestroList($maestroName); loadMaestroList(maestroName);
} }
function loadMaestroList($maestroName) { function loadMaestroList($maestroName) {
@@ -43,6 +43,7 @@
// console.log(xhr.responseText.length); // console.log(xhr.responseText.length);
if(xhr.responseText.length === 0 || xhr.responseText === null) { if(xhr.responseText.length === 0 || xhr.responseText === null) {
console.log("no data from server"); console.log("no data from server");
$("#error_message").text("서버에서 요청을 수행하지 못했습니다.");
return; return;
} }
@@ -51,16 +52,19 @@
if(replyJSON === null) { if(replyJSON === null) {
console.log("no data from server"); console.log("no data from server");
$("#error_message").text("서버에서 데이터가 넘어오지 않았습니다.");
return; return;
} }
if(replyJSON["ERROR"] !== undefined || replyJSON["RESULT"] === undefined) { if(replyJSON["ERROR"] !== undefined || replyJSON["RESULT"] === undefined) {
console.log(replyJSON["ERROR"]); console.log(replyJSON["ERROR"]);
$("#error_message").text(replyJSON["ERROR"]);
return; return;
} }
if(replyJSON["RESULT"] === "fail") { if(replyJSON["RESULT"] === "fail") {
console.log(replyJSON["RESULT"]); console.log(replyJSON["RESULT"]);
$("#error_message").text(replyJSON["ERROR"]);
return; return;
} }
@@ -138,6 +142,7 @@
// console.log(xhr.responseText.length); // console.log(xhr.responseText.length);
if(xhr.responseText.length === 0 || xhr.responseText === null) { if(xhr.responseText.length === 0 || xhr.responseText === null) {
console.log("no data from server"); console.log("no data from server");
$("#error_message").text("서버에서 요청을 수행하지 못했습니다.");
return; return;
} }
@@ -146,20 +151,24 @@
if(replyJSON === null) { if(replyJSON === null) {
console.log("no data from server"); console.log("no data from server");
$("#error_message").text("서버에서 데이터가 넘어오지 않았습니다.");
return; return;
} }
if(replyJSON["ERROR"] !== undefined || replyJSON["RESULT"] === undefined) { if(replyJSON["ERROR"] !== undefined || replyJSON["RESULT"] === undefined) {
console.log(replyJSON["ERROR"]); console.log(replyJSON["ERROR"]);
$("#error_message").text(replyJSON["ERROR"]);
return; return;
} }
if(replyJSON["RESULT"] === "fail") { if(replyJSON["RESULT"] === "fail") {
console.log(replyJSON["RESULT"]); console.log(replyJSON["RESULT"]);
$("#error_message").text(replyJSON["ERROR"]);
return; return;
} }
loadMaestroList(""); loadMaestroList("");
$("#error_message").text("마에스트로 계정이 정상적으로 등록되었습니다.");
} }
} }
} }
@@ -177,19 +186,24 @@
</header> </header>
<section id="section"> <section id="section">
<span id="search_maestro_name"> <div id="error_message">
<h1>검색</h1> </div>
마에스트로 이름 <div>
<input type="text" id="search_name"><input type="submit" name="찾기" onClick="searchMaestro()"><br/> <span id="search_maestro_name">
<br/> <h1>검색</h1>
</span>
<span id="search_result"> 마에스트로 이름
검색 결과 <input type="text" id="search_name"><input type="submit" name="찾기" onClick="searchMaestro()"><br/>
<div id="maestro_list_result_contents"> <br/>
</div> </span>
</span>
<span id="search_result">
검색 결과
<div id="maestro_list_result_contents">
</div>
</span>
</div>
</section> </section>
</div> </div>
+26 -28
View File
@@ -10,6 +10,7 @@
<script type="text/javascript" src="./../../../resources/jquery/jquery-3.3.1.min.js"></script> <script type="text/javascript" src="./../../../resources/jquery/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="./../js/main.js"></script> <script type="text/javascript" src="./../js/main.js"></script>
<script type="text/javascript" src="./../js/request_server.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
sessionStorage.setItem("maestroID", -1); sessionStorage.setItem("maestroID", -1);
@@ -21,30 +22,25 @@
let name = $("#maestro_name").val(); let name = $("#maestro_name").val();
let password = $("#password").val(); let password = $("#password").val();
let xhr = new XMLHttpRequest(); //new로 생성.
xhr.open('POST', './../server/maestro/login.php', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("maestro_name=" + name + "&password=" + password);
xhr.onload = function() {
if(xhr.readyState === 4 && xhr.status === 200) {
let replyJSON = JSON.parse(xhr.responseText);
// console.log(replyJSON);
// if(replyJSON !== null) sendRequestServer( //url, sendParams, onSuccess, onFail, isDebugMode);
// console.log(replyJSON); "./../server/maestro/login.php",
"maestro_name=" + name + "&password=" + password,
// if(replyJSON["MaestroID"] !== undefined) (jsonData) => {
// console.log(replyJSON["MaestroID"]); maestroID = jsonData["MaestroID"];
sessionStorage.setItem("maestroID", maestroID);
location.href = 'admin_register_list.html';
},
if(replyJSON !== null && replyJSON["MaestroID"] !== undefined && replyJSON["MaestroID"] !== null) { (errorMessage) => {
maestroID = replyJSON["MaestroID"]; if($("#error_message").length) {
sessionStorage.setItem("maestroID", maestroID); $("#error_message").text(errorMessage);
location.href = 'admin_register_list.html';
} else {
$("#error_message").val(replyJSON["ERROR"]);
} }
} }
}
);
} }
</script> </script>
@@ -54,17 +50,19 @@
<body bgcolor="white"> <body bgcolor="white">
<div> <div>
관리자 아이디 관리자 아이디
<input type="text" id="maestro_name" name="maestro_name"><br/> <input type="text" id="maestro_name" name="maestro_name"><br/>
암호 암호
<input type="password" id="password" name="password"><br/> <input type="password" id="password" name="password"><br/>
</div> </div>
<div class="col alignCenter"> <div class="col alignCenter">
<a id="login" class="btn btn-sea width400" onClick="login()">로그인</a> <a id="login" class="btn btn-sea width400" onClick="login()">로그인</a>
</div> </div>
<div id="error_message">
</div>
</body> </body>
-1
View File
@@ -34,4 +34,3 @@ function logout() {
sessionStorage.setItem("maestroID", maestroID); sessionStorage.setItem("maestroID", maestroID);
location.href = './../main/index.html'; location.href = './../main/index.html';
} }
+63
View File
@@ -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);
}
-10
View File
@@ -1,10 +0,0 @@
<?php
function send_error_code($error_code) {
$returnValue = array(
'error' => $error_code
);
echo json_encode($returnValue, JSON_UNESCAPED_UNICODE);
}
?>
+35
View File
@@ -0,0 +1,35 @@
<?php
$jsonReply = null;
function set_error_message($message) {
global $jsonReply;
$jsonReply["error"] = $message;
}
function set_data($name, $data) {
global $jsonReply;
$jsonReply[$name] = $data;
}
function send_result_success() {
global $jsonReply;
global $db_conn;
$jsonReply["result"] = "success";
echo json_encode($jsonReply, JSON_UNESCAPED_UNICODE);
$db_conn->close();
}
function send_result_fail() {
global $jsonReply;
global $db_conn;
$jsonReply["result"] = "fail";
echo json_encode($jsonReply, JSON_UNESCAPED_UNICODE);
$db_conn->close();
}
?>
+15 -15
View File
@@ -1,9 +1,11 @@
<?php <?php
header('Content-Type: application/json'); header('Content-Type: application/json');
include "./../lib/send_reply_json.php";
include "./../setup/connect_db.php";
$maestro_name = $_POST["maestro_name"]; $maestro_name = $_POST["maestro_name"];
$password = $_POST["password"]; $password = $_POST["password"];
/* /*
if(!is_numeric($enterCode)) { if(!is_numeric($enterCode)) {
send_error_code('생년월일이 숫자가 아닙니다. : '.$enterCode); send_error_code('생년월일이 숫자가 아닙니다. : '.$enterCode);
@@ -14,28 +16,26 @@ if(!is_numeric($enterCode)) {
} }
*/ */
include "./../setup/connect_db.php";
$maestro_id = get_maestro_id($maestro_name); $maestro_id = get_maestro_id($maestro_name);
if($maestro_id === null) { if($maestro_id === null) {
$replyJSON["ERROR"] = "마에스트로 아이디가 없습니다"; set_error_message("등록되지 않은 마에스트로 아이디니다.");
echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE); send_result_fail();
$db_conn->close();
exit; exit;
} }
$replyJSON = login($maestro_name, $password); $login_data = login($maestro_name, $password);
if($replyJSON === null || $replyJSON[MaestroID] === null) { if($login_data === null || $login_data[maestroID] === null) {
$replyJSON["ERROR"] = "비밀번호를 잘못 입력하셨습니다."; set_error_message("비밀번호가 틀렸습니다.");
echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE); send_result_fail();
$db_conn->close();
exit; exit;
} }
echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE); set_data("maestroId", $login_data[maestroID]);
$db_conn->close(); send_result_success();
exit; exit;
function get_maestro_id($maestro_name) { function get_maestro_id($maestro_name) {
global $db_conn; global $db_conn;
@@ -65,8 +65,8 @@ function login($maestro_name, $password) {
$stmt->fetch(); $stmt->fetch();
$stmt->close(); $stmt->close();
$replyJSON["MaestroID"] = $maestro_id; $result["maestroID"] = $maestro_id;
return $replyJSON; return $result;
} }
?> ?>
+8 -19
View File
@@ -1,7 +1,7 @@
<?php <?php
// load service db setting
$hasServiceDBSetting = file_exists("./../setup/service_db_setting.php"); $hasServiceDBSetting = file_exists("./../setup/service_db_setting.php");
if($hasServiceDBSetting == true) { if($hasServiceDBSetting == true) {
include("./../setup/service_db_setting.php"); include("./../setup/service_db_setting.php");
} else { } else {
@@ -11,27 +11,16 @@ if($hasServiceDBSetting == true) {
$db_name = "jisangs"; $db_name = "jisangs";
} }
// connect db
$db_conn = new mysqli($db_host, $db_user, $db_passwd, $db_name); $db_conn = new mysqli($db_host, $db_user, $db_passwd, $db_name);
if ($db_conn->connect_error) { if ($db_conn->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; exit;
} } else {
$query = "USE jisangs";
$query = "USE jisangs"; $result = mysqli_query($db_conn, $query);
$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();
} }
?> ?>