我正在尝试将一些数据发布到我的服务器并获取一些数据。出于某种原因,我发布的数据以 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
),但似乎没有任何效果。