[英]javascript sends data to php by XMLHttpRequest,also want the php send some data back
I create a xhr in js to send some data to a php file. 我在js中创建了一个xhr,以将一些数据发送到php文件。 The php file will insert these data into database.
php文件会将这些数据插入数据库。 Now I need to know the auto increased ID.
现在,我需要知道自动增加的ID。 I use
我用
echo mysql_insert_id();
to get the ID in php. 在PHP中获取ID。 How can I read this value in javascript?
如何在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);
You can attach an event handler to listen for the onreadystatechange
event. 您可以附加事件处理程序以侦听
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.