javascript通过XMLHttpRequest将数据发送到php,还希望php将一些数据发送回php
我在js中创建了一个xhr,将一些数据发送到php文件。php文件将把这些数据插入数据库。现在我需要知道我使用的自动增加IDjavascript通过XMLHttpRequest将数据发送到php,还希望php将一些数据发送回php,php,javascript,sql,json,xmlhttprequest,Php,Javascript,Sql,Json,Xmlhttprequest,我在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();
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 );
}
}
您应该考虑为XHR使用JavaScript框架。这让事情变得容易多了。看一看。下面是如何在jquery的第三行中创建一个xml,xmlhttp应该是xml。非常感谢。