Javascript json_在json stringify后解码无法正常工作

Javascript json_在json stringify后解码无法正常工作,javascript,php,jquery,json,Javascript,Php,Jquery,Json,我有一个javascript数组,它是json编码并发布到PHP的。 当我在过帐前检查数组中的值时 ieconsole.log(选择[878][2824])我得到一个值作为其结果。然后使用json.stringify对变量进行json编码并发布到服务器。但是当我用php解码并打印相同的值时,我得到的是空数组 js脚本 console.log(selection); $http({ method: 'POST', ur

我有一个javascript数组,它是json编码并发布到PHP的。 当我在过帐前检查数组中的值时 ie
console.log(选择[878][2824])
我得到一个值作为其结果。然后使用
json.stringify
对变量进行json编码并发布到服务器。但是当我用php解码并打印相同的值时,我得到的是空数组

js脚本

    console.log(selection);
    $http({
                method: 'POST',
                url: 'example.php',
                data: 'selection='+JSON.stringify(selection),
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })
$selectionData = json_decode($_POST['selection']);
print_r($selectionData[878][2824]);
php脚本

    console.log(selection);
    $http({
                method: 'POST',
                url: 'example.php',
                data: 'selection='+JSON.stringify(selection),
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })
$selectionData = json_decode($_POST['selection']);
print_r($selectionData[878][2824]);

那么在这里对数据进行编码时会发生什么呢。它是如何丢失的?

在使用POST时,您不必对内容进行字符串化,只需设置正确的标题即可

$http({
            method: 'POST',
            url: 'example.php',
            data: selection,
            headers: {'Content-Type': 'application/json'}
        })
或者,如果您需要带有标识符的:

$http({
            method: 'POST',
            url: 'example.php',
            data: {"selection": selection },
            headers: {'Content-Type': 'application/json'}
        })
$selectionData = $_POST['selection'];
在PHP中(如果您使用的是附加标识符):


似乎数据的格式不正确,我认为您必须将字符串的格式设置为

"selection :"  +JSON.stringify(selection);
试试这个

js

php


你能在$_POST['selection']中提供json字符串吗?看起来你漏掉了一个逗号,“'selection='+json.stringify(selection)…@amdixon我还有一些其他数据,与用户id类似。@MuhammadAli抱歉,我复制时出错了。”。