【问题标题】:php json_decode and Jquery Nestablephp json_decode 和 Jquery Nestable
【发布时间】:2014-01-20 19:10:28
【问题描述】:

我正在使用 jquery Nestable (http://dbushell.github.io/Nestable/)。

当我更改可嵌套元素的顺序时,我试图从中截取数据..

$('.dd').nestable().on('change', function() {
  var json_text = $('.dd').nestable('serialize');
  $.post("/includes/processes/core.php", {
    cmd: 'nestable',
    table: table,
    data: json_text
  }, function(response){
    alert(response);
  });
});

在 core.php 中我已经完成了:

$data = $_POST['data'];
$data = json_decode($data, true);
function parseJsonArray($jsonArray, $parentID = 0) {
  $return = array();
  foreach ($jsonArray as $subArray) {
    $returnSubSubArray = array();
    if (isset($subArray['children'])) {
  $returnSubSubArray = parseJsonArray($subArray['children'], $subArray['id']);
    }
    $return[] = array('id' => $subArray['id'], 'parentID' => $parentID);
    $return = array_merge($return, $returnSubSubArray);
  }
  return $return;
}

$readbleArray = parseJsonArray($data);

我必须操作这个数组,但我不能继续,因为响应是:

"json_decode() expects parameter 1 to be string, array given in core.php on line (where is "$data = json_decode($data, true;")"

如果我用 json_encode($data) 更改 json_decode($data, true) 这是响应:

"Invalid argument supplied for foreach() in core.php on line (where is "foreach ($jsonArray as $subArray);")"

请帮帮我..

【问题讨论】:

  • 尝试查看 var_dump( $_POST );它应该为您自己调试提供一些见解
  • $data 是一个数组,也许你可以马上使用这个数组
  • 谢谢...我正在努力...

标签: php jquery jquery-nestable


【解决方案1】:

尝试删除此行,因为 $.post 不进行 json 编码。

$data = json_decode($data, true);

【讨论】:

    【解决方案2】:

    使用 JSON.stringify。它会将您的 javascript 对象转换为 JSON 字符串,这将允许您发布它:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

    您可以在 PHP 中对其进行解码。为此,请在您的 jQuery 代码中替换:

    data: json_text
    

    与:

    data: {json_text: JSON.stringify(json_text)}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 2013-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多