[英]Ajax JSON.stringify, POST variable is empty
I'm trying to serialize an HTML form and to send it via Jquery with a POST action, currently I have the following Jquery: 我正在尝试序列化HTML表单,并使用POST操作通过Jquery发送它,目前我有以下Jquery:
var dataToSend = {
'name': 'person',
'description': 'very nice person'
}
$.ajax({
type: "POST",
url: "http://localhost/rest/PersonPOST.php",
data: JSON.stringify(dataToSend)
//contentType: 'application/json',
//dataType: 'json'
});
on the server side I have a PHP script which prints what it receives, so far I managed to receive a request without the $_POST variable. 在服务器端,我有一个PHP脚本,可以打印接收到的内容,到目前为止,我设法接收了一个没有$ _POST变量的请求。 If I decomment contentType and dataType nothing changes...
如果我将contentType和dataType分解,则没有任何变化...
<?php
error_log("START POST");
foreach ($_POST as $key => $entry)
{
if (is_array($entry))
{
foreach ($entry as $value)
{
error_log($key . ": " . $value . "<br>");
}
}
else
{
error_log($key . ": " . $entry . "<br>");
}
}
?>
What is wrong with the above ajax request? 上面的ajax请求出了什么问题?
EDIT : file_get_contents('php://input') at the server side correctly prints the content which should be inside $_POST variable. 编辑 :服务器端的file_get_contents('php:// input')正确打印应在$ _POST变量内的内容。 Can anyone answer how to normally put this inside the $_POST variable or why it's not possible?
谁能回答通常将其放在$ _POST变量中的原因,或者为什么不可能呢? thank you
谢谢
You dont need to stringify
since you are already manually creating a JSON object. 您无需进行
stringify
因为您已经在手动创建JSON对象。 Try it like this: 像这样尝试:
var dataToSend = {
'name': 'person',
'description': 'very nice person'
};
$.ajax({
type: "POST",
url: "http://localhost/rest/PersonPOST.php",
data: dataToSend
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.