使用jquery ajax向PHP发布JSON。

时间:2022-10-08 18:12:56

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???

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

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.

我尝试使用fireBug进行检查,我可以看到POST请求被正确地发送了,当php脚本被调用时,他会向我响应noooooooooob,看起来任何POST变量都已经设置好了。

All I want is to have my array =)

我想要的是数组=)

JSON String generated by JSON.stringify:

由JSON.stringify生成的JSON字符串:

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

JavaScript

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

save_categories.php

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

4 个解决方案

#1


17  

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

如果您删除了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);
    }
  });
});

#2


4  

dataType is json, so jQuery posts this:

数据类型为json,因此jQuery发布如下:

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

This is not standard urlencoded, so $_POST is empty.

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

You can set data to your complex structure, and jQuery will correctly encode it:

您可以将数据设置为复杂的结构,jQuery将正确地对其进行编码:

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

And in php: $categories = json_decode(file_get_contents('php://stdin'));

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

#3


0  

Try this:

试试这个:

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

I changed just this line:

我只改变了这句话:

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

because thats the way, how you have to write data there. I tested it, its working...

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

#4


0  

it's work for me you can try this. JavaScript

这是我的工作,你可以试试。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); } }); });

$(' #保存').click(函数(){ $。ajax({类型:'POST',内容类型:'application/json', url: 'save_categories '。php,数据类型:json,数据:json。stringify({'categories': $('.dd').nestable('serialize')}), success: function(msg) {alert(msg);} });});

you need to write the below code on save_categories.php $_POST is pre-populated with form data.

您需要在save_categories上编写下面的代码。php $_POST预填充表单数据。

To get JSON data (or any raw input), use php://input.

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

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

#1


17  

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

如果您删除了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);
    }
  });
});

#2


4  

dataType is json, so jQuery posts this:

数据类型为json,因此jQuery发布如下:

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

This is not standard urlencoded, so $_POST is empty.

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

You can set data to your complex structure, and jQuery will correctly encode it:

您可以将数据设置为复杂的结构,jQuery将正确地对其进行编码:

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

And in php: $categories = json_decode(file_get_contents('php://stdin'));

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

#3


0  

Try this:

试试这个:

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

I changed just this line:

我只改变了这句话:

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

because thats the way, how you have to write data there. I tested it, its working...

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

#4


0  

it's work for me you can try this. JavaScript

这是我的工作,你可以试试。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); } }); });

$(' #保存').click(函数(){ $。ajax({类型:'POST',内容类型:'application/json', url: 'save_categories '。php,数据类型:json,数据:json。stringify({'categories': $('.dd').nestable('serialize')}), success: function(msg) {alert(msg);} });});

you need to write the below code on save_categories.php $_POST is pre-populated with form data.

您需要在save_categories上编写下面的代码。php $_POST预填充表单数据。

To get JSON data (or any raw input), use php://input.

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

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