使用 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),
因为这就是你必须在那里写入数据的方式。我测试了它,它的工作...
- 如何使用jquery返回php-json数组对象
- 无法读取js中的php JSON编码数据
- PHP JSON编码输出与Javascript Ajax不兼容
- 如何在php JSON编码结果中包含谷歌分析
- jQuery在没有数据库的情况下从php-json自动完成
- AJAX PHP JSON编码未返回所有结果
- 将编码的 PHP JSON 传递给 JavaScript
- PHP json 格式编码
- 如何创建正确的 PHP JSON 响应
- 如何从 PHP JSON 编码制作这个 JavaScript 对象
- 钛 - PHP + JSON 解析错误
- php json - 仅从数组中获取第一个元素
- 创建一个 PHP json 提要并成功将其链接到 javascript
- DatePicker php json 不起作用
- 在 Javascript 中解析 PHP/JSON 数据
- 将 php JSON 对象转换为数组
- PHP JSON 不能回显值,总是返回一个数组
- 为什么 php json 响应不会将数据数组发送回 ajax jquery 请求
- PHP JSON Returns u0000
- 在 JavaScript 中使用 php json 结果