[英]javascript sends data to php by XMLHttpRequest,also want the php send some data back
我在js中创建了一个xhr,以将一些数据发送到php文件。 php文件会将这些数据插入数据库。 现在,我需要知道自动增加的ID。 我用
echo mysql_insert_id();
在PHP中获取ID。 如何在javascript中读取此值?
var xhr = new XMLHttpRequest();
xhr.open(
"POST",
"xxx.php",true
);
var postContainer = new Array();
postContainer.push(
{
data1 : value1,
data2 : value2,
});
var newJ = JSON.stringify(postContainer);
xhr.setRequestHeader("Content-type","application/json");
xhr.send(newJ);
您可以附加事件处理程序以侦听onreadystatechange
事件。
xhr.onreadystatechange = function(){
// check to make sure response finished and status is 200 meaning OK
if ( xhr.readyState == 4 && xhr.status == 200 ){
console.log( xhr.responseText );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.