通过ajax发送post数据,php无法获取
加下面一行代码试试,一段要加在send()方法前面
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
<input type="text" name="name" value="" id="inp"><span id="s"></span>
var inp=document.getElementById('inp');
inp.onblur=function(){
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
console.log(xhr.responseText);
if(xhr.responseText==1){
document.getElementById('s').innerHTML="<font color='red'>不可用</font>";
}else{
document.getElementById('s').innerHTML="<font color='green'>可用</font>";
}
}
}
xhr.open('post','http://127.0.0.1/cheshi/0002.php');
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
xhr.send('name='+inp.value+'&sex="男"');
}
<?php
if($_POST['name']=='admin'){
echo 1;
}else{
echo $_POST['sex'];
}
?>