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

时间:2014-06-19 08:40:50

标签: 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 Call:

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");
    }
}

我想要做的是登录控制台&#39;&#39; Hello World&#39;&#39; PHP处理完结果后回复。

PS:其实我想用一个参数&#39;&#39; hello world&#39;&#39;但如果我弄清楚如何得到回应,那么这样做应该是微不足道的。

6 个答案:

答案 0 :(得分:3)

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

您的XMLHttpRequest()对象名为ajax而不是xmlHttp,因此您从ajax对象而不是xmlHttp中选择responsea和readyState。

此外,您应该在告知XMLHttpRequest对象在接收到reslat时如何处理后,移动ajax.send(params);

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);
}

答案 1 :(得分:1)

目前尚不清楚问题是什么......

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

如果您只是在xmlHttp.readyState == 4内执行操作会怎样?

答案 2 :(得分:1)

您的回复文字似乎存储在xmlHttp.responseText中,因此您可以参与:

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

答案 3 :(得分:1)

您给定代码中的

xmlHttp似乎未定义。尝试用实际的ajax对象替换它:

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

答案 4 :(得分:0)

您应该ajax.send(params);之后加ajax.onreadystatechange = function () {/*...*/};

答案 5 :(得分:0)

如果我理解这一点,你应该将console.log(响应)放在你实际收到回复的地方,即

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