이것은 일부 데이터를 저장하는 내 PHP 함수입니다.
case "start_question":
$user_id = "123";
$p_id = $_POST[""];
$question_id = $_POST["question_Id"];
$answer = $_POST["answer_String"];
$counter = $_POST["counter"];
$points = $_POST["answer_Points"];
$user = new User($uid);
$user ->end_question($p_id,$user_id,$question_id,$answer,$counter,$points);
echo "Hello World";
break;
}
그리고 이것은 JS Ajax 호출입니다.
function startQuestion(){
var question_Id = questions_array[question_counter].question_Id;
console.log("Start Question",question_Id);
var ajax = new XMLHttpRequest();
var params = 'question_Id=' + question_Id;
ajax.open("POST", "ajax_controller.php?m=start_question", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.send(params);
ajax.onreadystatechange = function () {
console.log(response);
var response = "";
if (xmlHttp.readyState == 1) {
response += "Status 1: Server connection established ! <br/>";
} else if (xmlHttp.readyState == 2) {
response += "Status 2: Request recieved ! <br/>";
} else if (xmlHttp.readyState == 3) {
response += "Status 3: Processing Request ! <br/>";
} else if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var text = xmlHttp.responseText;
response += "Status 4: Processing Request ! <br/>";
response += text;
} else {
alert("Something is wrong !");
}
}
}
//If an error occur during the ajax call.
if (ajax.readyState == 4 && ajax.status == 404) {
console.log("Error during AJAX call");
}
}
제가하고 싶은 것은 PHP가 결과 처리를 마친 후 콘솔에``Hello World ''응답을 다시 로그인하는 것입니다.
추신 : 사실``hello world ''매개 변수를 사용하여 함수를 실행하고 싶지만 응답을 다시받는 방법을 알아 내면이 작업을 수행하는 것은 간단합니다.
기본적으로 완전히 확인하지 않은 복사 / 붙여 넣기의 오타 일 것입니다.
귀하의 XMLHttpRequest의 () 객체가 호출 ajax
되지 xmlHttp
는 XMLHTTP를 귀하의 아약스 개체에서 responsea 및 readyState가 데리러하지 않도록.
또한 ajax.send(params);
respose가 수신 될 때 수행 할 작업을 XMLHttpRequest 객체에 지정한 후에 실행할을 이동해야합니다 .
function startQuestion(){
var question_Id = questions_array[question_counter].question_Id;
console.log("Start Question",question_Id);
var ajax = new XMLHttpRequest();
var params = 'question_Id=' + question_Id;
ajax.open("POST", "ajax_controller.php?m=start_question", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.onreadystatechange = function () {
console.log(response);
var response = "";
if (ajax.readyState == 1) {
response += "Status 1: Server connection established ! <br/>";
} else if (ajax.readyState == 2) {
response += "Status 2: Request recieved ! <br/>";
} else if (ajax.readyState == 3) {
response += "Status 3: Processing Request ! <br/>";
} else if (ajax.readyState == 4) {
if (ajax.status == 200) {
var text = ajax.responseText;
response += "Status 4: Processing Request ! <br/>";
response += text;
} else {
alert("Something is wrong !");
}
}
}
//If an error occur during the ajax call.
if (ajax.readyState == 4 && ajax.status == 404) {
console.log("Error during AJAX call");
}
ajax.send(params);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다