【问题标题】:XmlHttpRequest POST data is emptyXmlHttpRequest POST 数据为空
【发布时间】:2014-12-22 22:16:55
【问题描述】:

我正在尝试通过 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。

【问题讨论】:

  • 什么是test.php打印?
  • 空数组Array ( )

标签: javascript php arrays ajax post


【解决方案1】:

你没有设置内容类型

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

【讨论】:

  • 你必须在open()之后但在send()之前调用setRequestHeader()
猜你喜欢
  • 2011-03-05
  • 2020-02-01
  • 2013-06-09
  • 1970-01-01
  • 2016-12-05
相关资源
最近更新 更多