Javascript XMLHttpRequest()未发布数据
我的javascript在Javascript XMLHttpRequest()未发布数据,javascript,Javascript,我的javascript在checkTests.php上,它的函数如下 function confirmedUpload(){ //Make the button so it can't be clicked again document.getElementById('confirmUpload').setAttribute("disabled", true); //This is the data we
checkTests.php
上,它的函数如下
function confirmedUpload(){
//Make the button so it can't be clicked again
document.getElementById('confirmUpload').setAttribute("disabled", true);
//This is the data we are posting
var postData = JSON.stringify(<?php echo $uploadJSON; ?>);
//This bit works OK and I see the string of JSON printed in the console.
console.log(postData);
//Create an XMLHttpRequestObject
var xhr = new XMLHttpRequest();
xhr.open("POST", "/uploadTests.php", true);
xhr.setRequestHeader( "Content-Type", "application/json" );
xhr.send(postData);
//Wait for the page to respond.
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4 && xhr.status == 200)
{
console.log(this.responseText);
}
}
}
为什么
uploadTests.php
认为$\u POST是空的?我认为您需要将内容类型
设置为应用程序/x-www-form-urlencoded
,以便php读取$\u POST
超全局数组。否则,内容将显示在PHP标准输入体中,可通过php://input
。有关更多详细信息,请参阅
编辑:
如果无法更改内容类型,则需要从
php://input
将检索请求内容正文的流。“我认为您需要将内容类型设置为application/x-www-form-urlencoded”-如果您的内容不是application/x-www-form-urlencoded,这是不好的!我很遗憾地通知您,更改内容类型并没有什么不同:/@Quentin然后OP需要从标准输入流中读取php://input
…@HarveyFletcher查看更新的编辑。$uploadJSON中有什么内容?您可能更希望先尝试发送硬编码字符串。
<?php
print_r($_POST);
echo ' ' . sizeof($_POST);
?>