을 처음 사용하는 동안 AJAX
이것이 제대로 작동하지 않는 이유를 모르겠습니다. 클릭 이벤트 리스너를 통해 편집 가능한 테이블이 있고 엔터 키의 키 누름을 수신하는 또 다른 이벤트 리스너가 있으며 발생하면 다른 변수를 가져와 AJAX 게시 요청을 서버에 보냅니다. 그러나 Enter 키를 누르면 아무 일도 일어나지 않습니다. 아무 일도 일어나지 않는다는 것은 서버의 PHP 기능이 나를 다른 곳으로 안내하는 대신 내가 있던 페이지에 머물러 있다는 것을 의미합니다. 이상한 점은 내 ajax.send(vars)
, 내가 console.logged 내 ajax
객체를 로그 아웃 하고 응답에 HTML 페이지가 제공되는
것으로 나타 났지만, 1. 내 ID, loc 또는 열 값이 비어있는 경우 얻을 수있는 응답을 보여 주었다는 것입니다. 나는 그 변수를 로그 아웃했지만 그렇지 않았습니다)
2. 해당 페이지가 제공되지 않았습니다. ajax 객체는 응답을 보여 주었지만 나 (최종 사용자)는 그 응답을 보지 못했습니다.
내 JavaScript는 다음과 같습니다.
index.addEventListener("keypress", function(e){
var key = e.keyCode;
if (key === 13) {
var entry = e.path[0].innerText;
var tabID = e.path[1].id;
var table = tabID.charAt(0);
if (table === 'o') {
table = "otheraccounts";
} if (table === 'wh') {
table = "wheatstoneaccounts";
} if (table === 'wo') {
table = "wideorbitaccounts";
}
var ID = tabID.charAt(1);
var column = e.path[0].id;
var loc = e.path[3].getElementsByTagName("input").location.value;
var ajax = new XMLHttpRequest();
ajax.open("post", "../other/index.php?action=updateTable", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.send(entry, table, ID, column, loc);
}
}, false)
그리고 여기 있습니다 PHP
:
case 'updateTable':
$column = filter_input(INPUT_POST, 'column', FILTER_SANITIZE_STRING);
$ID = filter_input(INPUT_POST, 'ID', FILTER_SANITIZE_NUMBER_INT);
$table = filter_input(INPUT_POST, 'table', FILTER_SANITIZE_STRING);
$entry = filter_input(INPUT_POST, 'entry', FILTER_SANITIZE_STRING);
$location = filter_input(INPUT_POST, 'loc', FILTER_SANITIZE_STRING);
if (empty($ID) || empty($column) || empty($table)){
$message = "<p class='message'>Stop tampering with my IDs!!</p>";
$_SESSION['message'] = $message;
header("Location: /radiosite/other/index.php?action=$location");
exit;
}
$result = updateEntry($column, $ID, $table, $entry);
if ($result === 1) {
$message = "<p class='message'>The entry was successfully updated in the database</p>";
$_SESSION['message'] = $message;
header("Location: /radiosite/other/index.php?action=$location");
exit;
} else {
$message = "<p class='message'>Nothing was added to the database</p>";
$_SESSION['message'] = $message;
header("Location: /radiosite/other/index.php?action=$location");
exit;
}
break;
AJAX는 리디렉션 헤더를받을 때 브라우저를 리디렉션하지 않습니다. 그것은 응답 데이터를 얻기 위해 대체 URL로 이동하도록 지시합니다.
새 URL을 문자열로 반환하도록 PHP를 변경합니다. 다음과 같이 줄을 변경하십시오.
header("Location: /radiosite/other/index.php?action=$location");
에:
echo "/radiosite/other/index.php?action=$location";
그런 다음 자바 스크립트 코드에서 응답을 읽고 window.location
리디렉션을 수행하도록 할당 할 수 있습니다 .
또한 POST 매개 변수를 제대로 보내지 않습니다. URL로 인코딩 된 문자열을 구성해야합니다.
var ajax = new XMLHttpRequest();
ajax.open("post", "../other/index.php?action=updateTable", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.send('entry=' + encodeURIComponent(entry) + '&table=' + encodeURIComponent(table) + '&ID=' + encodeURIComponent(ID) + '&column=' + encodeURIComponent(column) + '&loc=' + encodeURIComponent(loc));
ajax.onload = function() {
window.location = ajax.responseText.trim();
};
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다