发布JSON使用jQuery AJAX到PHPjQuery、JSON、PHP、AJAX

2023-09-10 14:11:10 作者:为自己丶拼个未来

我有去code我的JSON字符串,通过使用Ajax,并加盖结果一个简单的PHP文件,但我不能保证 $ _ POST 可变的,为什么???

我尝试检查与萤火虫,我可以看到POST请求正确,发送时在 PHP 脚本被调用时,他回应Noooooooob对我来说,它似乎任何职位变量被设置。

我要的是有我的数组=)

所产生的

JSON字符串 JSON.stringify

  [
   {
      ID:21,
      孩子:
         {
            ID:196
         },
         {
            ID:195
         },
         {
            ID:49
         },
         {
            ID:194
         }
      ]
   },
   {
      ID:29,
      孩子:
         {
            ID:184
         },
         {
            ID:152
         }
      ]
   },
   ...
]
 

JavaScript的

  $('#保存)。点击(函数(){
  VAR TMP = JSON.stringify($('DD')嵌套('序列化'));
  // TMP值: [{"id":21,"children":[{"id":196},{"id":195},{"id":49},{"id":194}]},{"id":29,"children":[{"id":184},{"id":152}]},...]
  $阿贾克斯({
    键入:POST,
    网址:save_categories.php,
    数据类型:JSON,
    数据:{类:TMP},
    成功:函数(MSG){
      警报(味精);
    }
  });
});
 

save_categories.php

 < PHP
  如果(使用isset($ _ POST ['类'])){
    $ JSON = $ _ POST ['类'];
    后续代码var_dump(json_de code($ JSON,真));
  } 其他 {
    回声Noooooooob;
  }
?>
 
jquery的ajax加载json文件 为什么在网页加载不出来啊

解决方案

如果您删除的dataType你的code工作:JSON,只是测试它

  $('#保存)。点击(函数(){
  VAR TMP = JSON.stringify($('DD')嵌套('序列化'));
  // TMP值: [{"id":21,"children":[{"id":196},{"id":195},{"id":49},{"id":194}]},{"id":29,"children":[{"id":184},{"id":152}]},...]
  $阿贾克斯({
    键入:POST,
    网址:save_categories.php,
    数据:{类:TMP},
    成功:函数(MSG){
      警报(味精);
    }
  });
});
 

I have a simple php file that decode my json string, passed with ajax, and stamp the result, but I can't keep the $_POST variable, why???

I try to inspect with fireBug and I can see that the POST request is sent correctly, when the php script is called, he respond Noooooooob to me, it seem any POST variable is set.

All I want is to have my array =)

JSON String generated by JSON.stringify:

[
   {
      "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";
  }
?>

解决方案

Your code works if you remove dataType: 'json', just tested it.

$('#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);
    }
  });
});