JSFiddle - React, Tailwind, and code Playground
by kirito0709
HTML
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$data = file_get_contents("php://input");// по ссылке есть объяснения этого способа https://qna.habr.com/q/683969
$POST = json_decode($data);
if (json_last_error() !== JSON_ERROR_NONE) {
echo "json_last_error() !== JSON_ERROR_NONE";
$res['status'] = 'json_last_error';
die(header('HTTP/1.0 415 Unsupported Media Type'));
}
JavaScript
async function sendForm2Popup(){
let name = '';
let email = '';
let city = '';
let cid = '';
try {
let reqbody = {name:name,email:email,city:city,cid:cid,};
let response = await fetch('/subscibe/', {
method: 'POST',
headers: {'Content-Type': 'application/json;charset=utf-8'},
body: JSON.stringify(reqbody)
});
if (response.ok) { // если HTTP-статус в диапазоне 200-299
let result = await response.json();
console.log(result);
}else{
//console.log("Ошибка HTTP: " + response.status);
console.log('HTTP 200 false');
}
} catch (e) {
console.log(e);
console.log('sendForm2Popup запрос fetch не удался');
}
}