[英]Data apparently is not being sent over AJAX POST - what am I doing wrong?
EDIT: I've updated my code due to feedback from here, and it's no longer giving me an empty array but now it's simply giving me null.编辑:由于来自这里的反馈,我已经更新了我的代码,它不再给我一个空数组,但现在它只是给我 null。
EDIT 2: Someone in my organization solved this for me.编辑 2:我组织中的某个人为我解决了这个问题。 Turns out I was using POST options that I didn't need: My working code is:
原来我使用的是不需要的 POST 选项:我的工作代码是:
Frontend:前端:
$.ajax({
type: "POST",
url: "update_data_ajax.php",
data: {
events: JSON.stringify(currentEventsRows)
}
}).done(function(data) {
console.log("Done");
console.log(data);
});
Backend:后端:
echo json_encode($_POST);
Original post:原帖:
I'm trying to make an AJAX POST request to post some data to a SQL table, but it doesn't seem like my POST request is actually sending anything because the PHP backend is simply giving me a null. I'm trying to make an AJAX POST request to post some data to a SQL table, but it doesn't seem like my POST request is actually sending anything because the PHP backend is simply giving me a null.
To try to narrow down the problem, I simplified my page down to a JavaScript frontend that posts an array of data and a PHP backend that simply returns the data.为了缩小问题的范围,我将页面简化为发布数据数组的 JavaScript 前端和仅返回数据的 PHP 后端。 Here's the relevant part of my frontend webpage (update_data.php):
这是我的前端网页(update_data.php)的相关部分:
let currentEventsRows = [/* array of dicts of strings */];
$.ajax({
method: "POST",
url: "update_data_ajax.php",
contentType: "application/json; charset=utf-8",
dataType: "JSON",
data: JSON.stringify{
events: currentEventsRows
},
processData: false
}).done(function(data) {
console.log("Done");
console.log(data);
});
Here's my PHP backend (update_data_ajax.php):这是我的 PHP 后端(update_data_ajax.php):
<?php
$_POST = json_decode(file_get_contents("php://input"), true);
echo $_POST;
?>
And here's the console output:这是控制台 output:
Done
null
What am I doing wrong?我究竟做错了什么? I feel like I'm missing something totally simple.
我觉得我错过了一些完全简单的东西。
You have 2 issues here, 1. You aren't sending the JSON properly.您在这里有 2 个问题,1. 您没有正确发送 JSON。 2. You're not reading the JSON properly.
2. 您没有正确阅读 JSON。
You have to encode your JSON to send in your ajax call您必须对 JSON 进行编码才能发送 ajax 呼叫
let currentEventsRows = [/* array of dicts of strings */];
$.ajax({
method: "POST",
url: "update_data_ajax.php",
contentType: "application/json; charset=utf-8",
dataType: "JSON",
data: JSON.stringify({
events: currentEventsRows
})
// processData: false -- not needed since the JSON is a string
}).done(function(data) {
console.log("Done");
console.log(data);
});
Your JSON data is not populated in the $_post superglobal(this only happens for multipart/form-data and application/x-www-form-urlencoded), so you have to read it from php://input
您的 JSON 数据未填充到 $_post 超全局中(这只发生在 multipart/form-data 和 application/x-www-form-urlencoded 中),因此您必须从
php://input
中读取它
<?php
echo file_get_contents('php://input');
?>
Someone from my organization actually solved this for me.我组织中的某个人实际上为我解决了这个问题。 Turns out I was using POST options that I didn't need.
原来我正在使用我不需要的 POST 选项。
Frontend:前端:
$.ajax({
type: "POST",
url: "update_data_ajax.php",
data: {
events: JSON.stringify(currentEventsRows)
}
}).done(function(data) {
console.log("Done");
console.log(data);
});
Backend:后端:
echo json_encode($_POST);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.