【发布时间】:2018-12-18 14:20:26
【问题描述】:
我正在尝试使用以下脚本向我的服务器发送一个字符串:
var xhr = new XMLHttpRequest();
xhr.open('POST', 'execute.php', true);
var data = 'name=John';
xhr.send(data);
但是,在服务器端,当 execute.php 被执行时,
isset($_POST['name'])
它返回false。这是对服务器的唯一请求。
为什么没有设置$_POST['name'] 以及如何解决它?
【问题讨论】:
-
HMLHttpRequest是这里的拼写错误,还是您有这样的错误?不应该是XMLHttpRequest吗? -
是的,对不起,我在原始代码中正确使用了它。
-
我认为您缺少请求标头...尝试:xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); (在您发送数据之前)
标签: javascript php post xmlhttprequest