我使用 POST 通过 AJAX 将数据提交到 php 文件。
仅提交字符串就可以正常工作,但现在我想使用 JSON 提交 JS 对象并在 PHP 端对其进行解码。
在控制台中我可以看到,我的数据已正确提交,但在 PHP 端 json_decode 返回 NULL。
我尝试过以下方法:
this.getAbsence = function()
{
alert(JSON.stringify(this));
jQuery.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "ajax/selectSingle.php?m=getAbsence",
data: JSON.stringify(this),
success : function(data){
alert(data);
}
});
}
PHP:
echo $_POST['data'];
echo json_decode($_POST['data']);
echo var_dump(json_decode($_POST['data']));
And:
this.getAbsence = function()
{
alert(JSON.stringify(this));
jQuery.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "ajax/selectSingle.php?m=getAbsence",
data: {'Absence' : JSON.stringify(this)},
success : function(data){
alert(data);
}
});
}
PHP:
echo $_POST['Absence'];
echo json_decode($_POST['Absence']);
echo var_dump(json_decode($_POST['Absence']));
警报只是为了检查一切是否正常......
是的,通常的字符串被正确回显:-)