本文共 902 字,大约阅读时间需要 3 分钟。
我使用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']));
和:
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']));
警报只是检查一切还好…
是的,通常的字符串被正确地回显了:-)
转载地址:http://fksxl.baihongyu.com/