使用 jquery ajax 将 JSON 发布到 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19976627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:18:59  来源:igfitidea点击:

Posting JSON with jquery ajax to PHP

javascriptphpjqueryajaxjson

提问by The_Guy

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

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

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

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

All I want is to have my array =)

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

JSON String generated by JSON.stringify:

生成的 JSON 字符串JSON.stringify

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

回答by melc

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);
    }
  });
});

回答by Marek

dataType is json, so jQuery posts this:

dataType 是 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.

这不是标准的 urlencoded,所以 $_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'));

回答by Vipin Kumar

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); } }); });

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

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://input。

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

回答by Legionar

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

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