使用 jquery ajax 发布 JSON 到 PHP


Posting JSON with jquery ajax to PHP

我有一个简单的 php 文件,可以解码我的 json 字符串,使用 ajax 传递,并标记结果,但我不能保留 $_POST 变量,为什么???

我尝试使用 fireBug 进行检查,我可以看到 POST 请求已正确发送,当调用 php 脚本时,他向我响应 Noooooooob,似乎设置了任何 POST 变量。

我想要的只是拥有我的数组 =)

JSON.stringify生成的 JSON 字符串:

[
   {
      "id":21,
      "children":[
         {
            "id":196
         },
         {
            "id":195
         },
         {
            "id":49
         },
         {
            "id":194
         }
      ]
   },
   {
      "id":29,
      "children":[
         {
            "id":184
         },
         {
            "id":152
         }
      ]
   },
   ...
]

JavaScript

$('#save').click(function() {
  var tmp = JSON.stringify($('.dd').nestable('serialize'));
  // tmp value: [{"id":21,"children":[{"id":196},{"id":195},{"id":49},{"id":194}]},{"id":29,"children":[{"id":184},{"id":152}]},...]
  $.ajax({
    type: 'POST',
    url: 'save_categories.php',
    dataType: 'json',
    data: {'categories': tmp},
    success: function(msg) {
      alert(msg);
    }
  });
});

save_categories.php

<?php
  if(isset($_POST['categories'])) {
    $json = $_POST['categories'];
    var_dump(json_decode($json, true));
  } else {
    echo "Noooooooob";
  }
?>

如果你删除dataType: 'json',你的代码就可以工作了,刚刚测试了它。

$('#save').click(function() {
  var tmp = JSON.stringify($('.dd').nestable('serialize'));
  // tmp value: [{"id":21,"children":[{"id":196},{"id":195},{"id":49},{"id":194}]},{"id":29,"children":[{"id":184},{"id":152}]},...]
  $.ajax({
    type: 'POST',
    url: 'save_categories.php',
    data: {'categories': tmp},
    success: function(msg) {
      alert(msg);
    }
  });
});
对我来说

你可以试试这个。JavaScript

$('#save').click(function() { $.ajax({ type: 'POST', contentType: 'application/json', url: 'save_categories.php', dataType: 'json', data: JSON.stringify({'categories': $('.dd').nestable('serialize')}), success: function(msg) { alert(msg); } }); });

您需要在save_categories.php上编写以下代码 $_POST 预先填充了表单数据。

若要获取 JSON 数据(或任何原始输入),请使用 php://input。

$json = json_decode(file_get_contents("php://input"));
$categories = $json->categories;

dataType 是 json,所以 jQuery 发布了这个:

{"categories":"[{'"id'":21,'"children'":[{'"id'":196},{'"id'":195},{'"id'":49},{'"id'":194}]},{'"id'":29,'"children'":[{'"id'":184},{'"id'":152}]},...]"}

这不是标准的 urlencoded,所以 $_POST 是空的。

你可以将数据设置为你的复杂结构,jQuery会正确地编码它:

$('#save').click(function() {
  $.ajax({
    type: 'POST',
    url: 'save_categories.php',
    dataType: 'json',
    data: $('.dd').nestable('serialize'),
    success: function(msg) {
      alert(msg);
    }
  });
});

在 php 中:$categories = json_decode(file_get_contents('php://stdin'));

试试这个:

$('#save').click(function() {
  var tmp = JSON.stringify($('.dd').nestable('serialize'));
  // tmp value: [{"id":21,"children":[{"id":196},{"id":195},{"id":49},{"id":194}]},{"id":29,"children":[{"id":184},{"id":152}]},...]
  $.ajax({
    type: 'POST',
    url: 'save_categories.php',
    dataType: 'json',
    data: 'categories=' + encodeURIComponent(tmp),
    success: function(msg) {
      alert(msg);
    }
  });
});

我只更改了这一行:

data: 'categories=' + encodeURIComponent(tmp),

因为这就是你必须在那里写入数据的方式。我测试了它,它的工作...