gpt4 book ai didi

php - JQuery Mobile POST 数据在 $_POST 中为空

转载 作者:行者123 更新时间:2023-12-04 05:29:09 25 4
gpt4 key购买 nike

我遇到了 JQuery Mobile(对我来说是新手)和 AJAX 调用的问题。我正在使用以下代码:

$.ajax({
type: "POST",
url: "http://**correct url**/post/todoitem",
beforeSend: addHeaders,
dataType: "json",
contentType: "application/json",
data: { "todoitem":"test" }, // this is just as a test
success: function(result) {
alert("Success: " + JSON.stringify(result));
},
error: function() {
alert("Error: " + JSON.stringify(arguments));
}
});

在执行此操作时,它调用一个 PHP 脚本,我需要从 todoitem 获取数据,因此在本例中为字符串“text”(最后,将发送多个变量,但现在我只使用一个参数简单)。

我的 PHP 代码如下所示(也仅用于测试目的):
echo json_encode($_POST));

结果是:没有,空。 $_POST 似乎是空的。我已经搜索并尝试了很多东西,但大多数答案(甚至在 stackoverflow 上)都是关于表单的,人们说我需要序列化表单的内容。但是,我根本没有使用表单。

我也试过
data: JSON.stringify({ "todoitem" : "test" })

正如一些人所建议的那样,但这也不起作用。
我确实知道由于我尝试过的这个小小的 PHP hack,数据正在被传输:
echo file_get_contents('php://input');

这正好显示了数据:todoitem = test。那么这一切到底错在哪里呢?我已经为此工作了好几天了!提前谢谢

最佳答案

问题在于您的代码的这一部分:

contentType: "application/json",`

删除该行应该使发送的 Content-Type header 默认为 application/x-www-form-urlencoded PHP 会将请求解码为 $_POST .

关于php - JQuery Mobile POST 数据在 $_POST 中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12840571/

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