gpt4 book ai didi

php - 使用 $.ajax 发送数据后如何从 $_POST 中获取数据

转载 作者:行者123 更新时间:2023-12-01 04:52:00 25 4
gpt4 key购买 nike

我正在使用ajax发送数据:

$('article.work a').click(function(e){
var h = $(this).attr('href');

e.preventDefault();

$.ajax({
type: "POST",
url: h,
data: workItems,
success: function(data){
console.log('success');
window.location = h;
},
error: function(){
console.log('eror');
}
});
});

它发送正常,在检查 firebug post 选项卡后,它看起来像:

Post value

所以我相信它正在发送正确的数据。但是当我尝试通过 php 从 $_POST 检索它时,我做到了

<?php 
print_r(json_decode($_POST["json"]));
?>;

没有打印任何内容。

我做错了什么?为什么即使我的数据已发送,$_POST 也无法识别?

最佳答案

尝试:

var_dump($_POST['json'], true) 。这意味着结果将是关联数组,而不是对象。

尝试:

    $.ajax({
url: "h",
type: "post",
data: workItems,
});

并尝试使用 var_dump($_POST); 获取数据或作为关联数组:var_dump($_POST['workItems'], true); .

我认为在输出 JSON 结果之前应该将其正确格式化为 UTF8:

$result = mb_convert_encoding($result,'UTF-8','UTF-8'); 
$result = json_decode($result);

将上面的代码放在输出 JSON 代码之前。

关于php - 使用 $.ajax 发送数据后如何从 $_POST 中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18674011/

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