通过 JQuery ajax.post 向 PHP 提交 JSON 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10947483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Submitting JSON data via JQuery ajax.post to PHP
提问by SamiSalami
Im submitting Data to a php file via AJAX using POST. It worked fine with just submitting strings, but now I wanted to submit my JS Object with JSON and decode it on PHP side.
我使用 POST 通过 AJAX 将数据提交到 php 文件。仅提交字符串就可以正常工作,但现在我想用 JSON 提交我的 JS 对象并在 PHP 端对其进行解码。
In the console I can see, that my data is submitted correctly but on PHP side json_decode returns NULL.
在控制台中,我可以看到,我的数据已正确提交,但在 PHP 端 json_decode 返回 NULL。
I've tried the following:
我尝试了以下方法:
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:
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:
PHP:
echo $_POST['Absence'];
echo json_decode($_POST['Absence']);
echo var_dump(json_decode($_POST['Absence']));
The alert was just to check everything is alright...
警报只是为了检查一切是否正常...
And yea usual string were echoed correctly :-)
是的,通常的字符串得到了正确的回应:-)
回答by UltraInstinct
Where you went wrong in your code in the first code is that you must have used this:
你在第一个代码中的代码出错的地方是你必须使用这个:
var_dump(json_decode(file_get_contents("php://input"))); //and not $_POST['data']
Quoting from PHP Manual
引用自 PHP 手册
php://input is a read-only stream that allows you to read raw data from the request body.
php://input 是一个只读流,允许您从请求正文中读取原始数据。
Since in your case, you are submitting a JSON in the body, you have to read it from this stream. Usual method of $_POST['field_name']wont work, because the post body is not in an URLencoded format.
由于在您的情况下,您要在正文中提交 JSON,因此您必须从此流中读取它。通常的方法$_POST['field_name']不起作用,因为帖子正文不是 URLencoded 格式。
In the second part, you must have used this:
在第二部分,你一定用过这个:
contentType: "application/json; charset=utf-8",
url: "ajax/selectSingle.php?m=getAbsence",
data: JSON.stringify({'Absence' : JSON.stringify(this)}),
UPDATE:
更新:
When request has a content type application/json, PHP wont parse the request and give you the JSON object in $_POST, you must parse it yourself from the raw HTTP body. The JSON string is retrieved using file_get_contents("php://input");.
当请求具有内容类型时application/json,PHP 不会解析请求并将 JSON 对象提供给您$_POST,您必须自己从原始 HTTP 正文中解析它。使用 检索 JSON 字符串file_get_contents("php://input");。
If you must get that using $_POSTyou would make it:
如果你必须使用$_POST它,你会做到:
data: {"data":JSON.stringify({'Absence' : JSON.stringify(this)})},
And then in PHP do:
然后在 PHP 中执行:
$json = json_decode($_POST['data']);
回答by YemSalat
Single quotes are not valid for php's json_encode, use the double quotes for both field names and values.
单引号对 php 无效,json_encode字段名称和值都使用双引号。
回答by Flygenring
To me, it looks like you should reformat your AJAX object. The url-property should only be the URL for the target php-file and any data that needs to be posted should be in the form of a query-string in the data-property. The following should work as you expected:
在我看来,您应该重新格式化 AJAX 对象。url-property 应该只是目标 php-file 的 URL,任何需要发布的数据都应该在 data-property 中采用查询字符串的形式。以下应该按您的预期工作:
this.getAbsence = function() {
var strJSONData = JSON.stringify(this);
alert(strJSONData);
jQuery.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: 'ajax/selectSingle.php',
data: 'm=getAbsence&Absence=' + strJSONData,
success: function(data) {
alert(data);
}
});
}
回答by Rishabh
try this
尝试这个
var vThis = this;
this.getAbsence = function()
{
alert(JSON.stringify(vThis));
jQuery.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "ajax/selectSingle.php?m=getAbsence",
data: JSON.stringify(vThis),
success : function(data){
alert(data);
}
});
}
EDIT
编辑
I think we can also do this!
我想我们也可以做到这一点!
var vThis = this;
this.getAbsence = function()
{
alert(JSON.stringify(vThis));
jQuery.ajax({
type: "POST",
dataType: "json",
url: "ajax/selectSingle.php?m=getAbsence",
data: vThis,
success : function(data){
alert(data);
}
});
}
and in PHP
在 PHP 中
print_r($_POST);

