javascript - 从纯 JS AJAX 调用返回响应 - POST

标签 javascript php ajax

这是我用来存储一些数据的 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”响应。

PS:实际上我想触发一个带有参数“hello world”的函数,但如果我弄清楚如何获取响应,那么这样做应该很简单。

最佳答案

它基本上只是一个可能来自复制/粘贴的拼写错误,您没有完全检查。

您的 XMLHttpRequest() 对象被称为 ajax 而不是 xmlHttp,因此您从 ajax 对象而不是 xmlHttp 中获取responsea和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);
}

关于javascript - 从纯 JS AJAX 调用返回响应 - POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24302306/

相关文章:

javascript - 将 python 请求转移到 node.js 请求时遇到问题

PHP 发送的电子邮件主题 - Hotmail/Outlook 将 £ 显示为 £

php - 我可以在升级前在 Linux 服务器上获得以前的 PHP 版本吗?

javascript - this.value 在 Firefox 上有效,但在 Internet Explorer 上无效?

javascript - 如何使用knockout.js恢复模板中的数据

javascript - 如何测试在异步测试中返回 promise 的 stub ?

javascript - $(this) 引用扩展 JQuery 方法中的第一个选择器元素

php - mysql-当我使用子查询时,为什么要花这么多时间来执行?

javascript - 无需表单即可在 ASP.NET 应用程序中注册新用户

javascript - jquery - ajax值为空后使div幻灯片向上