返回一个纯JS AJAX调用的响应- POST

Returning a response from a pure JS AJAX call - POST

本文关键字:调用 AJAX 响应 POST JS 一个 返回      更新时间:2023-09-26

这是我的PHP函数来存储一些数据:

case "start_question":
    $user_id = "123";
    $p_id = $_POST[""];
    $question_id = $_POST["question_Id"];
    $answer = $_POST["answerswer_String"];
    $counter = $_POST["counter"];
    $points = $_POST["answerswer_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"响应。

PS:实际上我想用参数"hello world"来触发一个函数,但如果我知道如何获得响应,那么这样做应该是微不足道的。

这基本上只是一个打印错误,可能是你没有完全检查的复制/粘贴。

您的XMLHttpRequest()对象被称为ajax而不是xmlHttp,所以您从ajax对象而不是xmlHttp中拾取响应和readyState。

你也应该移动ajax.send(params);运行后,你已经告诉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);
}

不清楚是什么问题…

xmlHttp.readyState == 4在请求完成时被触发,即当PHP完成处理您的请求时。

如果你只是在xmlHttp.readyState == 4中执行你的操作呢?

似乎您的响应文本存储在xmlHttp.responseText中,因此您可以工作到:

if (xmlHttp.status == 200) {
    console.log(xmlHttp.responseText);
    /* ... */
}

xmlHttp在您给定的代码中似乎是未定义的。尝试将其替换为实际的ajax对象:

if (ajax.readyState == 1){ ... }

你应该把ajax.send(params);放在ajax.onreadystatechange = function () {/*...*/}之后;

如果我理解正确的话,您应该将console.log (response)放在实际接收响应的位置,即

var text = xmlHttp.responseText;
                response += "Status 4: Processing Request ! <br/>";
                response += text;
                /**********************/
                console.log(response)
               /**********************/