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
+16 -2
View File
@@ -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,6 +186,10 @@
</header>
<section id="section">
<div id="error_message">
</div>
<div>
<span id="search_maestro_name">
<h1>검색</h1>
@@ -190,6 +203,7 @@
<div id="maestro_list_result_contents">
</div>
</span>
</div>
</section>
</div>
+16 -18
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="./../js/main.js"></script>
<script type="text/javascript" src="./../js/request_server.js"></script>
<script type="text/javascript">
$(document).ready(function() {
sessionStorage.setItem("maestroID", -1);
@@ -21,30 +22,25 @@
let name = $("#maestro_name").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)
// console.log(replyJSON);
sendRequestServer( //url, sendParams, onSuccess, onFail, isDebugMode);
"./../server/maestro/login.php",
"maestro_name=" + name + "&password=" + password,
// if(replyJSON["MaestroID"] !== undefined)
// console.log(replyJSON["MaestroID"]);
if(replyJSON !== null && replyJSON["MaestroID"] !== undefined && replyJSON["MaestroID"] !== null) {
maestroID = replyJSON["MaestroID"];
(jsonData) => {
maestroID = jsonData["MaestroID"];
sessionStorage.setItem("maestroID", maestroID);
location.href = 'admin_register_list.html';
} else {
$("#error_message").val(replyJSON["ERROR"]);
}
},
(errorMessage) => {
if($("#error_message").length) {
$("#error_message").text(errorMessage);
}
}
);
}
</script>
@@ -65,6 +61,8 @@
<a id="login" class="btn btn-sea width400" onClick="login()">로그인</a>
</div>
<div id="error_message">
</div>
</body>
-1
View File
@@ -34,4 +34,3 @@ function logout() {
sessionStorage.setItem("maestroID", maestroID);
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
header('Content-Type: application/json');
include "./../lib/send_reply_json.php";
include "./../setup/connect_db.php";
$maestro_name = $_POST["maestro_name"];
$password = $_POST["password"];
/*
if(!is_numeric($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);
if($maestro_id === null) {
$replyJSON["ERROR"] = "마에스트로 아이디가 없습니다";
echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE);
$db_conn->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;
}
?>
+6 -17
View File
@@ -1,7 +1,7 @@
<?php
// load service db setting
$hasServiceDBSetting = file_exists("./../setup/service_db_setting.php");
if($hasServiceDBSetting == true) {
include("./../setup/service_db_setting.php");
} else {
@@ -11,27 +11,16 @@ if($hasServiceDBSetting == true) {
$db_name = "jisangs";
}
// connect db
$db_conn = new mysqli($db_host, $db_user, $db_passwd, $db_name);
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;
}
} else {
$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();
}
?>