php 通过 ajax POST json 数据发送一个空数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8983247/
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
POST json data via ajax sends an empty array
提问by Mike
I'm trying to post data via ajax, this is my info:
我正在尝试通过 ajax 发布数据,这是我的信息:
var jsondata =
{"address" : [
{ "id": addid, "streetaddress": streetaddress, "city": city, "state": state, "zipcode": zipcode, "latitude": latitude},
]
};
var jsontosend = JSON.stringify(jsondata, null, 2);
ajax function:
阿贾克斯功能:
$.ajax({
type: "POST",
url: "process.php",
contentType: "application/json; charset=utf-8",
dataType: "JSON",
data: jsontosend,
success: function(msg){
alert(msg);
}
});
return false;
alert('Data sent');
}
}
on the php end, when i print_r($_POST) it just says
在 php 端,当我 print_r($_POST) 它只是说
array(0) {
}
I'm alerting (jsontosend) and its showing me everything perfectly, as well as in firebug using post mothod, its showing all the params sent in a perfect clean manner.
我正在提醒 (jsontosend) 并且它完美地向我展示了所有东西,以及使用 post 方法在 firebug 中,它显示了以完美干净的方式发送的所有参数。
The only way it passes any data is if I use GET method.
它传递任何数据的唯一方法是使用 GET 方法。
Any advice is greatly appreciated!
任何意见是极大的赞赏!
EDIT: adding POST data from firebug. this is whats being alerted from the alert function:
编辑:添加来自萤火虫的 POST 数据。这是警报功能发出的警报:
{"address":[{"id":1473294,"streetaddress":"3784 Howard Ave","city":"Washington DC","state":"DC","zipcode":20895,"latitude":39.027820587}]}
this is what firebug is showing as whats being passed when using POST method:
这是 firebug 在使用 POST 方法时显示的内容:
myData=%7B%0A++++%22address%22%3A+%5B%0A++++++++%7B%0A++++++++++++%22id%22%3A+76076%2C%0A++++++++++++%22streetaddress%22%3A+%223784+Howard+Ave%22%2C%0A++++++++++++%22city%22%3A+%22Washington+DC%22%2C%0A++++++++++++%22state%22%3A+%22DC%22%2C%0A++++++++++++%22zipcode%22%3A+20895%2C%0A++++++++++++%22latitude%22%3A+39.027820587%0A++++++++%7D%0A++++%5D%0A%7D
and this is the response for var_dump of $_POST:
这是对 $_POST 的 var_dump 的响应:
array(0) {
}
}
this is a var_dump of $_POST['myData']
这是 $_POST['myData'] 的 var_dump
NULL
采纳答案by Steven Wexler
I'm skeptical of the way you're using the contentType property. Try taking out contentType. The default content type is application/x-www-form-urlencoded (http://api.jquery.com/jQuery.ajax/).
我对您使用 contentType 属性的方式持怀疑态度。尝试取出 contentType。默认的内容类型是 application/x-www-form-urlencoded ( http://api.jquery.com/jQuery.ajax/)。
Also, use something like {mydata: jsontosend} for your data property.
此外,为您的数据属性使用类似 {mydata: jsontosend} 的内容。
$.ajax({
type: "POST",
url: "process.php",
//contentType: "application/json; charset=utf-8",
dataType: "JSON",
data: {mydata: jsontosend},
success: function(msg){
alert(msg);
}
});
回答by dkulkarni
Use
用
data:{myData: jsontosend}
data:{myData: jsontosend}
It should send myData as a parameter in your request.
它应该在您的请求中将 myData 作为参数发送。
回答by JayVee
PHP doesn't understand application/json requests (by default). See this question: How to post JSON to PHP with curl
PHP 不理解 application/json 请求(默认情况下)。看到这个问题:How to post JSON to PHP with curl
回答by carbontax
I found that the comment provided by dkulkarni was the best solution here. It is necessary to read directly from the input stream to get POST data of any complexity. Changing the value of $.ajax({contentType: ''})
did not work for me..
我发现 dkulkarni 提供的评论是这里最好的解决方案。有必要直接从输入流中读取以获取任何复杂度的 POST 数据。更改的值$.ajax({contentType: ''})
对我不起作用..
In order to troubleshoot this problem I had set up a controller method to echo back the JSON I was posting to my server. Changing the ContentType header made no difference. But after reading dkulkarni's comment I changed my server code from this:
为了解决这个问题,我设置了一个控制器方法来回显我发布到我的服务器的 JSON。更改 ContentType 标头没有任何区别。但是在阅读了 dkulkarni 的评论后,我将服务器代码更改为:
return $_POST;
to this:
对此:
$data = file_get_contents('php://input');
return json_decode($data);
It worked perfectly.
它工作得很好。
回答by Ixx
As dkulkarni commented in his post (Jan 24 at 7:53), the only way to retrieve JSON sent with Content-Type application/json seems to be to retrieve it directly from the input stream.
正如 dkulkarni 在他的帖子(1 月 24 日 7:53)中评论的那样,检索使用 Content-Type application/json 发送的 JSON 的唯一方法似乎是直接从输入流中检索它。
I made a lot of tests with different approaches, but always when I set Content-Type to JSON, the $_POST in PHP is empty.
我用不同的方法做了很多测试,但总是当我将 Content-Type 设置为 JSON 时,PHP 中的 $_POST 为空。
My problem is I need to set Content-Type to JSON, because the server (where I don't have access) expects it, otherwise it returns unsupported datatype. I suppose they are retrieving directly from input stream.
我的问题是我需要将 Content-Type 设置为 JSON,因为服务器(我无权访问)需要它,否则它返回不受支持的数据类型。我想他们是直接从输入流中检索的。
Why I have to reproduce that in own PHP file? Because I need to make a proxy for testing issues, but thats not the point of this thread.
为什么我必须在自己的 PHP 文件中重现它?因为我需要为测试问题制作一个代理,但这不是这个线程的重点。
回答by Subodh
Try removing ' dataType: "JSON" ' from the ajax request and check.
尝试从 ajax 请求中删除 ' dataType: "JSON" ' 并检查。
回答by bluegrass
Taking contentType out is not a good idea. You DO want to send this data in JSON format. Key is to do what has been mentioned above, instead of trying to access the data on the receiving end (the target php file) with $data= $_POST it is critical to use $data = file_get_contents('php://input');
去掉 contentType 不是一个好主意。您确实希望以 JSON 格式发送此数据。关键是要做上面提到的,而不是尝试使用 $data= $_POST 访问接收端(目标 php 文件)上的数据,使用 $data = file_get_contents('php://input' );
Here is an example of the entire round trip for a scenario with php and knockout.js:
以下是使用 php 和 Knockout.js 的场景的整个往返示例:
JS (on requesting page):
JS(在请求页面上):
function() {
var payload = ko.toJSON({ YOUR KNOCKOUT OBJECT });
jQuery.ajax({
url: 'target.php',
type: "POST",
data: payload,
datatype: "json",
processData: false,
contentType: "application/json; charset=utf-8",
success: function (result) {
alert(result);
}
});
};
And then in target.php (the page receiving the request):
然后在 target.php(接收请求的页面)中:
$data = file_get_contents('php://input');
$payload_jsonDecoded = json_decode($data