返回一个纯JS AJAX调用的响应- POST
Returning a response from a pure JS AJAX call - POST
这是我的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)
/**********************/
相关文章:
- 阻止在select2单击时调用ajax
- 调用Ajax并返回响应
- Ajax:只在元素存在的情况下调用Ajax
- PHP,MySQL,AJAX-调用AJAX结果后,Bootstrap CSS样式不会显示
- 调用Ajax内部的函数
- 无法获取属性'的值;拆分'在IE8中调用ajax之后
- 我们如何在Firefox中调用AJAX
- Rails:通过调用Ajax重定向
- Cookie 在 Spotify (Javascript) 中调用 ajax 后未保存
- 通过调用 Ajax 来设置 struts bean 值
- 无法从服务器中的 php 调用 Ajax 函数
- 如何在附加表行时调用 ajax
- 在facebook用户发送邀请请求后调用ajax调用
- 定期调用Ajax
- 调用Ajax生成的表单的值
- 无法调用ajax函数
- 在调用AJAX之后显示ng控制器的内容;作为“;作用
- CSS,Java脚本在php中调用ajax后无法正常工作
- 成功调用AJAX后,Toast Growl不会出现
- 如何在调用AJAX时记录或捕获信息和错误