Home > front end >  Why Ajax to send data failure, the background error 405
Why Ajax to send data failure, the background error 405

Time:03-12

Because it is a novice, in the following video to learn Ajax, before has tried the get method, the browser can be successful, use post to 405 error data request failed, output XHR. ReadyState is 2
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 file

CodePudding user response:

refer to 1st floor is fierce, response:
many servers are not allowed to POST request in response to a static file

That how to solve this problem, because is written to follow the teacher video, the teacher can
  • Related