2011-03-10 137 views
0

我用jQuery收集数据,把它放到一个多维数组中,使用JSON.stringify并通过AJAX传递给PHP,由于某种原因json_decode不断给我一个Syntax error, malformed JSON错误。通过Ajax到php的多维数组通过json_decode变成null

是被传递给PHP

[\"foo\",\"foobar did the baz\",[[\"bar\",\"kg\",\"200\"],[\"baz\",\"l\",\"1337\"]]] 

奇怪的

继承人的JSON是,我使用JSON.stringify在JS多维阵列上。继承人如何我把它放在一起

var dataz = []; 
var arrayContainingAll = []; 

$("li", "#ingredientlist").each(function() { 
    var tempArray = []; 
    tempArray.push($(".ingredientname", this).text()); 
    tempArray.push($(".unittext", this).text()); 
    tempArray.push($(".amounttext", this).text()); 
    arrayContainingAll.push(tempArray); 
}); 

dataz.push($("h1").text()); 
dataz.push($("#method").val()); 
dataz.push(arrayContainingAll); 
var json = JSON.stringify(dataz); 

我怎样才能让PHP正确解析多维数组? 我已经通过在3个不同的字符串化阵列固定它,但它更多的为什么多维数组失败

PHP的显示会发生什么好奇的是:var_dump(json_decode($_POST['ingredients']));

,因为它appearantly重要的是要展示如何我发布的数据,继承人的JS做Ajax请求

$.ajax({ 
    url: '/api/savenewrecipe.php', 
    type: 'POST', 
    data: 'ingredients=' + json + "&name=" + $("h1").text() + "&method=" + $("#method").val(), 
    success: function(result) { 
     if (result.ok == true) { 
      // @todo remove this for debugging purposes 
      //document.location.href = '/recipe/' + result.id; 
     } 
     else { 
      showError("Noget gik galt!", 2000); 
     } 
    } 

}); 
+0

你的php代码在哪里? – Neal 2011-03-10 21:17:49

+0

它应该是irelevant – 2011-03-10 21:20:09

+0

你在哪里发布到php在你的js? – Neal 2011-03-10 21:21:16

回答

2

如果服务器使用魔术引号,你需要将其删除:

if (get_magic_quotes_gpc()) { 
    $_POST['ingredients'] = stripslashes($_POST['ingredients']); 
} 
+0

我刚刚阅读评论部分,我认为你击败了我的答案=)+1 – kjy112 2011-03-10 21:43:41