Add: test codes for sending data from python to javascript (failed)

This commit is contained in:
2019-06-03 10:29:37 +09:00
parent e5244360c9
commit bda10ea105
2 changed files with 92 additions and 0 deletions
+70
View File
@@ -0,0 +1,70 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta charset="UTF-8">
<script type="text/javascript" src="./../../../resources/jquery/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
function getPath() {
return window.location.pathname;
}
function getDirectoryName(path) {
var pathAndFileNames = path.split("/");
return pathAndFileNames[pathAndFileNames.length - 2];
}
function getPHPPath(directoryName) {
if(directoryName === "test") // mouse_typing/test/
return "../src/web/";
else // mousetyping/src/web/client/
return "../";
}
function onSucceededListener(replyJSON) {
$('#data_from_python').empty();
$('#data_from_python').text(replyJSON);
}
function onFailedListener(replyJSON) {
$('#data_from_python').empty();
$('#data_from_python').text(replyJSON);
}
$(document).ready(function() {
var path = getPath();
var directoryName = getDirectoryName(path);
var phpPath = getPHPPath(directoryName);
var xhr = new XMLHttpRequest();
xhr.open("POST", phpPath + "python/chocomae/test_send_json_data.py", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
// var replyJSON = JSON.parse(xhr.responseText);
// if(replyJSON != null && replyJSON["PlayerID"] != null)
// onSucceededListener(replyJSON);
// else
// onFailedListener(replyJSON);
}
};
// xhr.send("maestro_name=" + maestroName + "&name=" + playerName + "&enter_code=" + enterCode);
xhr.send();
});
</script>
</head>
<body>
<div><h1>Data</h1></div>
<div id="data_from_python"></div>
</body>
</html>