XmlHttpRequest POST 数据为空

Mis*_*saz 5 javascript php arrays ajax post

我正在尝试通过 AJAX 通过 POST 方法将数据发送到 php。PHP代码:

\n\n
<?php\nprint_r($_POST);\n
Run Code Online (Sandbox Code Playgroud)\n\n

JavaScript 代码:

\n\n
var xml = new XMLHttpRequest();\nxml.open("POST", "test.php", false);\nxml.send("a=X");\ndocument.write(xml.responseText);\n
Run Code Online (Sandbox Code Playgroud)\n\n

结果是:

\n\n
Array ( )\n
Run Code Online (Sandbox Code Playgroud)\n\n

为什么数组 [a] => "X" 中没有\xc2\xb4t?数组每次都是空的。我在 Apache 2.4.10 (XAMPP v3.2.1) 上使用 PHP 5.6.3。

\n

Mus*_*usa 4

您没有设置内容类型

var xml = new XMLHttpRequest();
xml.open("POST", "test.php", false);
xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xml.send("a=X");
Run Code Online (Sandbox Code Playgroud)