jQuery .ajax POST返回null

时间:2018-03-08 19:49:54

标签: php json ajax post

我正在尝试将一些数据发布到我的服务器并获取一些数据。出于某种原因,我发布的数据以 null

的形式返回

jQuery:

var data = { message1: "bla", message2: "blabla" };

$.ajax({
    type: 'POST',
    url: 'https://example.com/api',
    data: JSON.stringify(data), // EDIT: replacing this with [data: data,] also does not help (also not in combination with omitting the following line)
    contentType: 'application/json', // EDIT: I have removed this line now, but the problem persists
    dataType: 'json',
    success: function(response) {
      console.log(response);
    }
  });

PHP:

$response['message1'] = $_POST['message1'];
$response['message2'] = $_POST['message2'];
$response['message3'] = 'blablabla';

print_r(json_encode($response));

返回:

  

{message: null ,message2: null ,message3:“blablabla”}

我想要:

  

{message:“bla”,message2:“blabla”,message3:“blablabla”}

我已在我的数据中尝试过各种引号组合(例如'{"message1": "bla" (...)}'并省略JSON.stringify),但似乎没有任何效果。

0 个答案:

没有答案