let person = {
name:"Vova",
last:"Nak",
age:18
}
function toPHP(){
var xhr = new XMLHttpRequest();
xhr.open("POST", "phpForm1.php");
xhr.setRequestHeader("Content-Type", "application/json");
person = JSON.stringify(person);
xhr.send(person);
xhr.onreadystatechange = function(){
if(xhr.readyState != 4) return;
if(xhr.status != 200){
console.log(xhr.status + ": " + xhr.statusText);
} else{
console.log("Hello", xhr.responseText);
document.getElementById("info").textContent = xhr.responseText;
}
}
}
document.getElementById("goP").addEventListener("click",toPHP)
let count = 2;
function query() {
count = count + 2;
let http = new XMLHttpRequest();
http.open("POST","phpForm1.php");
http.setRequestHeader("Content-Type","text/plain");
http.send(count);
http.onreadystatechange = function () {
if(http.readyState != 4) return false;
if(http.status != 200){
console.log(http.status + ": " + http.statusText);
}else{
console.log("hello",http.responseText);
}
}
}
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$headers = getallheaders();
if($headers['Content-Type']=='application/json') {
$data = file_get_contents('php://input');
$obj = json_decode($data);
$out = print_r($obj,true);
} else $out = 'Нет нечего!';
exit($out);
}
$connect = mysqli_connect("localhost","root","root","ajax ");
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$headers_1 = getallheaders();
if($headers_1['Content-Type'] == "text/plain"){
$str = "Okay";
$result = print_r($str,true);
}else $result = "No,it cant be";
exit($result);
}
?>
Здраствуйте еще раз всем,можете пожалуста подсказать каким способом наилучшее отличать запросы AJAX на стороне сервера???