我有一个使用JSON.stringify从JavaScript对象转换而来的JSON值.我正在尝试使用PHP解析JSON的内容,但是我没有任何运气.我确定我确实在做一些基本的错误.
在file1.php中,我有类似以下内容:
var irxmlnewsreleases = new Array();
irxmlnewsreleases[0]={
"attachmentfileid":12039
};
var news_release = JSON.stringify(irxmlnewsreleases);
$(document).ready(function () {
$("#response").text(news_release);
});
然后,我尝试使用file2.php中的json_decode从file1.php中读取此数据.
我首先尝试(错误地)使用file_get_contents,并且对此进行了一段时间的尝试,但没有成功.我想这个问题显然是在运行JavaScript之前JSON值不存在,因此PHP当然永远无法读取jQuery生成的div内容的值.我不知道如何获得这种价值.
JSON已在file1.php中成功生成,并且是有效的JSON(我已经通过jsonlint运行了它).
将动态生成的JSON的值转换为PHP的更好的方法是什么?
解决方法:
$(document).ready(function () {
$("#response").text(news_release);
$.post('file2.php', { php_post_var1: news_release }, function (data) {
//do something with the PHP script output here if you want
});
});
然后在您的PHP脚本file2.php中执行以下操作
$news_release = $_POST['php_post_var1'];
echo 'PHP received ' . $news_release;
?>
标签:json,php,jquery
来源: https://codeday.me/bug/20191201/2084013.html