Front End (JS):
A. Key points:
B. Complete code: <script>if (window. XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest ();} else{//code for IE6, IE5 xmlhttp=new activexobject ("Microsoft.XMLHTTP");} var async = Trueif (Async = = True) {Xmlhttp.onreadystatechange = function () {if (Xmlhttp.readystate < 4) { Console.log (' Loading ... ')}else if (xmlhttp.readystate = = 4 && Xmlhttp.status = =) {XM Ldoc=xmlhttp.responsetext; Console.log (xmldoc)}else{console.log (' fail ... ') Xmlhttp.abort ()}}}xmlhttp.open ( "POST", ' http://192.168.0.200/index.php ', async); xmlhttp.withcredentials = True;xmlhttp.setrequestheader (" Authorization "," Basic "+ Btoa (" User:pass ")); Xmlhttp.setrequestheader (" Content-type "," application/ X-www-form-urlencoded "); Xmlhttp.send (' Name=lee '); if (async = = = False) {Xmldoc=xmlhttp.responsetext; Console.log (xmldoc)}</script>
Back-end (PHP):<?phpif(!(isset($_SERVER[‘PHP_AUTH_USER‘],$_SERVER[‘PHP_AUTH_PW‘])&&$_SERVER[‘PHP_AUTH_USER‘]==‘user‘&&$_SERVER[‘PHP_AUTH_PW‘]==‘pass‘)){header(‘WWW-Authenticate:Basic realm ="Restricted area"‘);}else{ // 验证成功echo "Success";}
XMLHttpRequest Basic Certification