[英](AJAX/PHP) Why is my POST request not working in this brief example?
Here is my JavaScript: 这是我的JavaScript:
var xhr = new XMLHttpRequest();
xhr.open("POST", "pants.php", true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var slot = document.getElementsByTagName("section")[0].innerHTML = xhr.responseText;
}
}
xhr.send("name=Sarah");
I am sending "name=Sarah" to testpage.php via POST, and when I get a response, I'm doing to display it on my page. 我通过POST发送“ name = Sarah”到testpage.php,当我收到响应时,我正在做的就是在页面上显示它。
And here's pants.php: 这是长裤.php:
echo $_POST['name'];
So I should just be displaying "Sarah" on the page. 所以我应该只在页面上显示“ Sarah”。 But instead, I get the error ": Undefined index: name".
但是,相反,我得到了错误“:Undefined index:name”。 I can't seem to understand why this is...
我似乎无法理解这是为什么...
You should add the following: 您应该添加以下内容:
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
This header is mandatory for POST requests 对于POST请求,此标头是必需的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.