无法将JSON从Javascript解码为PHP

问题描述 投票:0回答:1

我正在ajax问题中将JSON从JavaScript传递到PHP。Json是这个(从PHP打印,不带json_decode):

[{"id":"440","step":"1","pass_rules":{"manual":false,"check_percent":true,"quiz":false,"list_element":["id1"]}},{"id":"438","step":"2","pass_rules":{"manual":false,"check_percent":true,"quiz":true,"list_element":["id1"]}}]

通过ajax:

    var formData = new FormData(this);   
    formData.append('array', JSON.stringify(fullArray));

    $.ajax({
            type: "POST",
            url: URL_API + "/call_to_api",
            data: formData,
            cache: false,
            contentType: false,
            processData: false, 
            success: function (json) {
            //code 
         }
       })

如果通过这种方式从PHP获取数组,我可以像在帖子顶部打印json一样打印json

 $array = mysql_real_escape_string($_POST['array']); 

json_decode在元素上不起作用,并打印为空。我不明白为什么!

更新:数组以这种方式在表单内传递:

array: [{"id":"441","step":"1","pass_rules":{"manual":false,"check_percent":true,"quiz":false,"list_video":[]}},{"id":"438","step":"3","pass_rules":{"manual":false,"check_percent":true,"quiz":true,"list_video":[]}}]

json_last_error()在这里调用:

$array= json_decode($_POST['array']); 
json_last_error();

json_last_error返回4,所以JSON_ERROR_SYNTAX ...但是JSON似乎不错:/

UPDATE2:var_dump输出:

string(388) "[{"id":"441","step":"1","pass_rules":{"manual":false,"check_percent":true,"quiz":false,"list_video":[]}},{"id":"438","step":"3","pass_rules":{"manual":false,"check_percent":true,"quiz":true,"list_video":[]}}]"
javascript php json ajax post
1个回答
0
投票

如@aynber所建议

$array = html_entity_decode($_POST['array']);
json_decode(array); 

解决问题!

© www.soinside.com 2019 - 2024. All rights reserved.