Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQueryAjax将JSON发布到PHP_Javascript_Php_Jquery_Ajax_Json - Fatal编程技术网

Javascript 使用jQueryAjax将JSON发布到PHP

Javascript 使用jQueryAjax将JSON发布到PHP,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,我有一个简单的php文件,可以解码我的json字符串,用ajax传递,并标记结果,但是我不能保留$\u POST变量,为什么 我尝试用fireBug检查,我可以看到POST请求被正确发送,当调用php脚本时,他向我响应nooob,似乎设置了任何POST变量 我只想让我的数组=) 由JSON.stringify生成的JSON字符串: [ { "id":21, "children":[ { "id":196 }

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

我尝试用fireBug检查,我可以看到POST请求被正确发送,当调用php脚本时,他向我响应nooob,似乎设置了任何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";
  }
?>

试试这个:

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

因为这就是你写数据的方式。我测试了它,它的工作…

数据类型是json,所以jQuery发布了以下内容:

{"categories":"[{\"id\":21,\"children\":[{\"id\":196},{\"id\":195},{\"id\":49},{\"id\":194}]},{\"id\":29,\"children\":[{\"id\":184},{\"id\":152}]},...]"}
这不是标准的urlencoded,因此$\u 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\u decode(文件\u获取\u内容('php://stdin'));

如果删除
数据类型:“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() {
  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);
    }
  });
});
$('#保存')。单击(函数(){
$.ajax({
键入:“POST”,
contentType:'应用程序/json',
url:“save_categories.php”,
数据类型:“json”,
数据:JSON.stringify({'categories':$('.dd').nestable('serialize')}),
成功:功能(msg){
警报(msg);
}
});
});

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

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


firebug这就是您在查询字符串参数中发送的内容?firebug告诉我categories参数传递正确,但当我访问$\u POST时,它找不到。删除回音“noooob”行:)这与ops代码没有什么不同。jQuery将把对象转换成字符串,就像您所做的那样。关键部分可能是URL编码,而不是将数据属性作为字符串传递。这个解决方案完全符合我的意思,非常感谢@melc,我还添加了一个preventDefault(),因为我使用了我认为这应该是答案,特别是对于php部分+从我这里得到1。