<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>测试AJAX</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
       <link href="css/main.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    
    <script>
function c(){
var username=document.getElementById("text").value;
var content=document.getElementById("p");	
		if(username.length==0){{content.innerHTML="用户名输入为空，请重新输入";return false;}}
	
	var request=new XMLHttpRequest();
	alert(username);
		request.open("GET","ajax.php?username="+username);
		request.send();
		request.onreadystatechange=function(){
			if(request.readyState===4 && request.status===200) //返回成功
			{
		//alert(request.responseText);
	//alert(request.readyState);
	//alert(request.status);
	
				content.innerHTML=request.responseText;
			}
			else
			{
			content.innerHTML="出现错误，AJAX失败"+request.readyState+"空格"+request.status;	
			//return false;
			}
		}
	
	
		
}
	</script>
    
    
    
    
  </head>
  <body>
  <div class="container">
    <h1 class="page-header">测试AJAX<small>对应的PHP文件为test.php</small></h1>
    <div class="form-inline">
    <input name="t" id="text" type="text" class="form-control" placeholder="请输入用户名">
    <input type="button" class="btn btn-primary" id="button" onclick="c()" value="立即查询">
    </div>
    <p class="jumbotron" id="p">
    这里显示服务器返回的内容
    
    </p>
    
    
    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>