判断json传到ajax为空,Ajax JSON.stringify,POST变量为空

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

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值