Good confused, to find the can't find a solution for a long time, please,
//1, to create an Ajax object
Var XHR=new XMLHttpRequest ();
//5, monitoring data
XHR. Onreadystatechange=function () {
If (XHR readyState!==4) {
The console. The log (XHR. ReadyState);
return;
}
If (XHR. The status & gt;=200 & amp; & XHR. The status & lt; {
=300)Var text=XHR. The responseText;
//convert json data type
Var resp.=JSON parse (XHR. The responseText);
If (resp. The result) {
Alert (" login succeeds ")
} else {
Alert (" login failed ")
}
} else {
The console. The error (" data request failed ");
}
}
//2, configure this object
XHR. Open (' post ', './tes. PHP ", true);
//3, set up a request header
XHR. SetRequestHeader (' the content-type ', 'application/x - WWW - form - urlencoded ");
//4, backend sends data
XHR. Send (' user=kamenrider& Password=555 ');
The following was written by yourself a PHP file
<? PHP
$user=$_POST [' user '].
$password=$_POST [' password '];
If ($user=='kamenrider & amp; & {$password=='555')
Echo '{" result ": true}';
} else {
Echo '{} "result" : false;
}
?>
CodePudding user response:
Many servers are not allowed to POST request in response to a static fileCodePudding user response: