【发布时间】:2016-04-19 00:15:56
【问题描述】:
我正在尝试通过 JSON.stringify() 将我的 javascript 对象发送到 PHP
Javascript:
$('#save').on('click touch', function(){
obj = {
"1" : {
"1" : "hey",
"2" : "hay"
},
"2" : {
"1" : "hey",
"2" : "hay"
}
}
var json = JSON.stringify( obj );
console.log(json)
$.ajax({
type: 'POST',
url: 'ajax.php',
success: function(data) {
alert(data);
$("p").text(data);
}
});
});
ajax.php:
<?php
$obj = json_decode($json);
echo $obj;
?>
但此代码返回一个错误,指出 $json 未定义。
我不知道为什么这不起作用。
【问题讨论】:
-
您尚未在
$.ajax选项上设置data属性。 -
POST 数据将在 PHP 中的
$_POST变量中可用 -
@csum: 仅当 OP 真的费心告诉 jquery USE ajax 调用中的
jsonvar ... -
@MarcB 是的,是的。这更像是一个笼统的陈述。 POST 数据在
$_POST -
我已经解决了这个问题。谢谢大家
标签: javascript php json ajax object