2 x923417164 x923417164 于 2017.09.15 22:29 提问

求叫:关于js中的AJAX
function unameExist(uname){
    var back;
    var xhr =new XMLHttpRequest();  
    xhr.onreadystatechange = function(){
        console.log(xhr.readyState);
        if(xhr.readyState == 4){
            if(xhr.status === 200){
                var list = JSON.parse(xhr.responseText);
                if(list.code == 1){
                    back = true;
                }else{ 
                    back = false;
                }
            }
        }
    }   
    xhr.open('post','unameCheck.php',true);
    xhr.setRequestHeader("Content-type","application/x-www-form-urlecode");
    xhr.send("name="+uname+"");
    return back
    };
    <?php
header("Content-type:text/plain;charset=utf-8");
$name = $_REQUEST['name'];
$conn = mysqli_connect('127.0.0.1','root','','my');
$sql = "SET NAMES UTF8";
mysqli_query($conn,$sql); 
$sql = "SELECT uname FROM login WHERE uname='$name'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_ALL($result,MYSQLI_ASSOC);
if($row){
    $output['code'] = 1;
    $output['msg'] = 'exist';
}else{
    $output['code'] = 2;
    $output['msg'] = 'no-exist';
}
echo Json_encode($output);
?>

为什么我的uname一直传不到我的PHP里面 是哪里写错了还是什么

1个回答

showbo
showbo   Ds   Rxr 2017.09.16 10:47

xhr.setRequestHeader("Content-type","application/x-www-form-urlecode");
==>
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");

caozhy
caozhy 正解,英文拼错了
2 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片