<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 = true
if(async === true){
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState < 4){
console.log(‘loading...‘)
}else if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
xmlDoc=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>
<?php
if(!(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";
}
原文地址:http://blog.51cto.com/12173069/2085960