<php>Ajax基本格式

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="jquery-1.11.2.min.js"></script>
</head>

<body>
<div>用户名:<input type="text" id="uid" /></div>
<div>密码:<input type="text" id="pwd" /></div>
<div><input type="button" value="登录" id="btn" /></div>
<div><input type="text" id="user" /><div id="ts"></div></div>
</body>
<script type="text/javascript">
    $(document).ready(function(e) {
           $("#btn").click(function(){
            var uid = $("#uid").val();
            var pwd = $("#pwd").val();
            $.ajax({
                url:"0405chuli.php",//处理页面的路径
                data:{u:uid,p:pwd,type:0},//通过json格式将一组数据传过去
                type:"POST",//数据的提交和传递方式,最好用POST,用POST也行
                dataType:"TEXT",//页面返回值的类型,共有三种:TEXT,JSON,XML可选
                success:function(data){//回调函数:如果ajax调用成功,就执行这个success后面的函数,(data)当做参数返回过来        
                        if(data=="OK!")
                        {
                            window.location = "0405main.php";    
                        }
                        else
                        {
                            alert(data);    
                        }
                }
                
            
            
                
            });
        })    
        $("#user").blur(function(){
            var user = $(this).val();
            $.ajax({
                url:"0405chuli.php",    
                data:{u:user,type:1},
                type:"POST",
                dataType:"TEXT",
                success: function(d){
                        if(d=="OK")
                        {
                            $("#ts").html("用户名可用!");
                            
                        }
                        else
                        {
                            $("#ts").html("<span style='color:red'>该用户名已存在</span>");    
                        }
                }
                
                
            });
            
        })
        
        
    });








</script>
</html>
原文地址:https://www.cnblogs.com/shark1100913/p/5355473.html