gpt4 book ai didi

php - 从 $_POST 中的 json 中读取关联数组

转载 作者:IT王子 更新时间:2023-10-29 00:04:17 24 4
gpt4 key购买 nike

我正在使用 jQuery 将 json 对象发布到我的 php 应用程序。

jQuery.post("save.php",JSON.stringify(dataToSend), function(data){ alert(data); });

从 firebug 中提取的 json 字符串如下所示

{ "data" : [ { "contents" : "This is some content",
"selector" : "DIV.subhead"
},
{ "contents" : "some other content",
"selector" : "LI:nth-child(1) A"
}
],
"page" : "about_us.php"
}

在 php 中,我试图将其转换为关联数组。

到目前为止我的 php 代码是

<?php
$value = json_decode(stripcslashes($_POST));
echo $value['page'];
?>

对 ajax 调用的响应应该是“about_us.php”,但它返回空白。

最佳答案

$_POST 如果请求正文不是标准的 urlencoded 形式,则不会被填充。

相反,从 read-only php://input stream 中读取像这样获取原始请求正文:

$value = json_decode(file_get_contents('php://input'));

关于php - 从 $_POST 中的 json 中读取关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5806971/

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