ajax 报错200 是用的json啊

一片枫叶ing 发布于 2017/05/06 11:36
阅读 256
收藏 0

这是HTML代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>注册</title>
    <script type="text/javascript" src="./jquery-1.10.1.min.js"></script>
</head>
<body class="login" >
<div class="mibox">
    <div class="" style="display: inline-block;">
        <div class="myformbd" style="background-size: 100% 100%;">
            <div style="width: 100%;text-align: left;">
                <label class="mylable">电话号码</label>
                <input class="myinput" id="phone" value="" placeholder="请输入电话"/><br/>
                <label class="mylable">密码</label>
                <input class="myinput" type="password" id="password" value="" placeholder="请输入密码"/><br/>
                <label class="mylable">请再次输入密码</label>
                <input class="myinput" type="password" id="andPassword" value="" placeholder="请再次输入密码"/><br/>
                <div style="width: 100% ;padding-top: 20px;">
                    <input class="mybutton" type="submit" id="submit" value="注册"/>
                    <input class="mybutton" type="submit" id="off" value="取消"/>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    (function(){
        $(document).keyup(function(event){
            if(event.keyCode ==13){
                login();
            }
        });
        $('#submit').bind('click', function(){
            login();
        });
    })();

    function login(){
        var url = '/register.php';
        var phone = $('#phone').val();
        var password = $('#password').val();
        var andPassword = $('#andPassword').val();
        $.ajax({
            data: {
                phone : phone,
                password : password,
                andPassword : andPassword,
            },
            dataType : 'json',
            success : function(data){
                    alert(data.msg);
            },
            error:function(xhr){
                alert(xhr.status);
            },
            type : 'post',
            url : url
        });
    }
</script>
</body>
</html>

这是PHP代码

<?php
header("Content-type:application/json;charset=utf-8");
    $phone = $_POST['phone'];
    $password = $_POST['password'];
    $andPassword = $_POST['andPassword'];
    if(!$phone){
        $return = array('status' => 1, 'msg' => '账号不能为空');
        echo json_encode($return);exit;
    }
    if(!$password){
        $return = array('status' => 1, 'msg' => '输入密码不能为空');
        echo json_encode($return);exit;
    }
    if(!$andPassword){
        $return = array('status' => 1, 'msg' => '再次输入密码不能为空');
        echo json_encode($return);exit; 
    }
    if($andPassword != $password){
        $return = array('status' => 1, 'msg' => '两次输入密码不同');
        echo json_encode($return);exit; 
    }
    if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
        $return = array('status' => 1, 'msg' => '电话号码不符合规范');
        echo json_encode($return);exit;
    }
    $phone = clean_xss($phone);
    $password = clean_xss($password);
    
    //sql部分,pdo处理
    $dbh = new PDO('mysql:host=localhost;dbname=user', 'root', '');
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
    $dbh->exec('set names utf8'); 
    $sql = "insert into TABLE(username, password) values(:username, :password)";
    $sql = $pdo->prepare($sql);
    $sql->execute(array(':username' => $username));
    $sql->execute(array(':password' => $password));
    if($sql->fetch(PDO::FETCH_ASSOC)){
        $return = array('status' => 1, 'msg' => '注册成功');
        print(json_encode($return));exit;
    }else{
        $return = array('status' => 0, 'msg' => '注册失败');
        print(json_encode($return));exit;
    }
    
    //防xss
    function clean_xss(&$string, $low = False){
        if (! is_array ( $string ))
        {
            $string = trim ( $string );
            $string = strip_tags ( $string );
            $string = htmlspecialchars ( $string );
            if ($low)
            {
                return True;
            }
            $string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string );
            $no = '/%0[0-8bcef]/';
            $string = preg_replace ( $no, '', $string );
            $no = '/%1[0-9a-f]/';
            $string = preg_replace ( $no, '', $string );
            $no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';
            $string = preg_replace ( $no, '', $string );
            return True;
        }
        $keys = array_keys ( $string );
        foreach ( $keys as $key )
        {
            clean_xss ( $string [$key] );
        }
    }

点击结果:报错200

加载中
0
OSC_rCCAaH
OSC_rCCAaH
200代码是http请求成功的意思!
0
大賢者
大賢者
该评论暂时无法显示,详情咨询 QQ 群:点此入群
0
无意苦争春
无意苦争春

200不是成功吗。。

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部