[英]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.