fetch('./main.php', {
method: 'post',
body: "test=1"
})
.then(response => response.text())
.then(function(data) {
console.log(data);
})
// main.php
var_dump($_POST);
function getFormData($method) {
// GET или POST: данные возвращаем как есть
if ($method === 'GET') return $_GET;
if ($method === 'POST') return $_POST;
// PUT, PATCH или DELETE
$data = array();
$exploded = explode('&', file_get_contents('php://input'));
foreach($exploded as $pair) {
$item = explode('=', $pair);
if (count($item) == 2) {
$data[urldecode($item[0])] = urldecode($item[1]);
}
}
return $data;
}
const formData = new URLSearchParams(new FormData(form)).toString();
const options = {
method: 'post',
body: formData,
headers: {
'X-Requested-With': 'XMLHttpRequest',
'content-type': 'application/x-www-form-urlencoded'
},
};
fetch(url, options)
.then(...