当到达php页面时,ajax @post是空的?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (34)

我有以下代码,尝试使用ajax / json将值发布到php文件。php文件会抛出警告:

Undefined index: companyid, Undefined index: companyname, Undefined index: calendar_eventStart, Undefined index: calendar_eventEnd.

使用ajax发布的变量在服务器端拾取时是空的,这是我的代码:

控制台日志:

Data: {"companyid":1,"companyname":"Fifteen AS","calendar_eventStart":"2018-06-06T22:00:00.000Z","calendar_eventEnd":"2018-06-06T22:00:00.000Z"}

jQuery:

var formData = {
                    companyid,
                    companyname,
                    calendar_eventStart,
                    calendar_eventEnd
                };
                formData = (JSON.stringify(formData));
                console.log('Data: ' + formData);                   
                $.ajax({
                url: "create_calendar_event.php",
                type: "post",
                dataType: "json",   
                data:{data: formData},                   
                success: function(response) {
                alert(response);                
                }               
                });

PHP:

var_dump($_POST['companyid']);
var_dump($_POST['companyname']);
var_dump($_POST['calendar_eventStart']);
var_dump($_POST['calendar_eventEnd']);
$event_status = 'Success';
echo json_encode($event_status,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);  
提问于
用户回答回答于

你可以做

$.ajax({
      url: "create_calendar_event.php",
      type: "post",
      dataType: "json",   
      data: formData, 
      success: function(response) {
           alert(response);                
      }               
});

扫码关注云+社区

领取腾讯云代金券