gpt4 book ai didi

javascript - JS : Processing form as a POST but retrieving as a GET?

转载 作者:行者123 更新时间:2023-11-30 08:36:39 25 4
gpt4 key购买 nike

用 PHP 检索数据,我不能使用 $_POST;但是 $_GET。为什么?我是否错误地发送了我的表单数据?

我原以为 request.open("POST" 会将表单作为 POST 而不是 GET 处理?我如何将其作为 POST 发送?

var request = new XMLHttpRequest();
request.open("POST","email.php?text=" + textarea.value + "&email=" + email.value, true);

request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var resp = request.responseText;

console.log(resp);

}
};

request.send();

最佳答案

因为您要在 URL 中添加数据。

将您的请求更改为:

request.open("POST","email.php", true);
request.setRequestHeader("Content-length", 2); // 2 here is the no. of params to send
....


request.send("text=" + textarea.value + "&email=" + email.value);

文档:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

关于javascript - JS : Processing form as a POST but retrieving as a GET?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30802852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com