【发布时间】:2018-09-08 15:45:09
【问题描述】:
我在checkTests.php 上有我的javascript,它的功能如下
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);
}
}
}
我希望将数据postData 发布到页面uploadTests.php,然后从控制台中的uploadTests.php 页面返回array(data)。
但实际发生的是发布请求,我看到了来自 javascript 的输出 console.log(postData);,但是当 uploadTests.php 返回值时,我得到的只是 array() 0 并且没有数据。
uploadTests.php的内容是:
<?php
print_r($_POST);
echo ' ' . sizeof($_POST);
?>
为什么uploadTests.php 认为 $_POST 是空白的?
【问题讨论】:
-
$uploadJSON 中有什么?您可能更愿意先尝试发送硬编码字符串。
标签: javascript