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表单并通过Jquery通过POST操作发送它,目前我有以下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. If I decomment contentType and dataType nothing changes...
在服务器端我有一个PHP脚本打印它收到的内容,到目前为止我设法接收没有$ _POST变量的请求。如果我取消注释contentType和dataType没有任何变化......
error_log("START POST");
foreach ($_POST as $key => $entry)
{
if (is_array($entry))
{
foreach ($entry as $value)
{
error_log($key . ": " . $value . "
");
}
}
else
{
error_log($key . ": " . $entry . "
");
}
}
?>
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. Can anyone answer how to normally put this inside the $_POST variable or why it's not possible? thank you
编辑:服务器端的file_get_contents('php:// input')正确打印应该在$ _POST变量内的内容。任何人都可以回答如何正常将其放入$ _POST变量或为什么不可能?谢谢
1 个解决方案
#1
2
You dont need to stringify since you are already manually creating a JSON object. Try it like this:
您不需要进行字符串化,因为您已经手动创建了JSON对象。试试这样:
var dataToSend = {
'name': 'person',
'description': 'very nice person'
};
$.ajax({
type: "POST",
url: "http://localhost/rest/PersonPOST.php",
data: dataToSend
});