Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript XMLHttpRequest()未发布数据_Javascript - Fatal编程技术网

Javascript XMLHttpRequest()未发布数据

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

我的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 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);

?>