XmlHttpRequest POST 数据为空

XmlHttpRequest POST data is empty

本文关键字:数据 POST XmlHttpRequest      更新时间:2023-09-26

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

<?php
print_r($_POST);

JavaScript 代码:

var xml = new XMLHttpRequest();
xml.open("POST", "test.php", false);
xml.send("a=X");
document.write(xml.responseText);

结果是:

Array ( )

为什么不在数组 [a] => "X" 中?数组每次都为空。我在Apache 2.4.10(XAMPP v3.2.1(上使用PHP 5.6.3。

您没有设置内容类型

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