问题描述
我有去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;
}
?>
如果您删除的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);
}
});
});
这篇关于发布JSON使用jQuery AJAX到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!