消息关闭
    暂无新消息!
想要实现的效果是输入账号和密码,通过ajax请求,php验证输入正确时返回json数据,通过jquery后登录框编程个人信息显示框,但是我点击登录按钮,得不到想要的效果,请问是哪里出了问题?
1、html用户登录框
<div class="loginBox loginBox2" >
         <div class="title_lg">用户登录</div>
         <div class="form">
                <p>
                    <label for="usernum">账&nbsp;号:</label>
                    <input type="text" id="usernum">
                </p>
                <p>
                    <label for="password">密&nbsp;码:</label>
                    <input type="password" id="password">
                </p>
                <div class="btns">
                    <a href="javascipt:;" class="btn loginBtn">登录</a>
                    <a href="register.html" class="btn registerBtn">注册</a>
                </div>
                <a href="javascipt" class="forget">忘记密码了?点击这里!</a>
            </div>
        </div>

2、jquery的ajax请求代码:
$(function(){
$('.loginBtn').click(function(){
var $usernum=$('#usernum').val();
var $password=$('#password').val();
if($usernum==""){
alert('请输入账号!');
$('#usernum').focus();
return false;
}
if($password==""){
alert('请输入密码!');
$('#password').focus();
return false;
}
$.ajax({ 
type: "POST", 
url: "login.php?action=login", 
dataType: "json", 
data: {"usernum":$usernum,"password":$password},
success: function(json){ 
if(json.success==1){
$(".loginBox2").css("display","none"); 
var div='<div class="loginBox infoBox">'+
'<div class="title_lg">个人信息</div>'+
'<div class="pensonalDetails">'+
'<img src="images/1.gif" alt="">'+
'<p class="welcome"><span>'+json.username+'</span>,欢迎你!</p>'+
'<ul class="detailsOperate">'+
'<li><i></i><a href="reset.html">修改密码</a></li>'+
'<li><i></i><a href="reset.html">修改资料</a></li>'+
'<li><i></i><a href="javascript:;">上传头像</a></li>'+
'<li><i></i><a href="index.html" id="logout">退出登录</a></li>'+
'</ul>'+
'</div>'+
'</div>';
$(".left").prepend(div); 
}else{  
/*$('.forget').html(json.msg); */      
return false; 


});
});
});
</script>

3、php代码
<?php
session_start();

//包含数据库连接文件
include('conn.php');
$action = $_GET['action']; 
if($action == 'login'){
$usernum = htmlspecialchars($_POST['usernum']);
$password = MD5($_POST['password']);

    $query = mysql_query("select * from userinfo where usernum='$usernum'"); 
$us = is_array($result = mysql_fetch_array($query));  
    $ps = $us ? $password == $result['password'] : FALSE; 
if ($ps){
            $_SESSION['usernum'] = $result['usernum'];
$_SESSION['username'] = $result['username'];
$arr['success'] = 1; 
$arr['msg']='登录成功!';
$arr['username'] = $_SESSION['username']; 
}
else { 
        $arr['success'] = 0; 
$arr['msg']='登录失败!';
        } 
echo json_encode($arr);
}
?>

4、数据库表如图

6个回答

︿ 1
Request URL:http://localhost:8078/byse/php/login.php?action=login
Request Method:POST
Status Code:200 OK
Remote Address:[::1]:8078
Response Headers
view source
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Length:0
Content-Type:text/html; charset=UTF-8
Date:Fri, 17 Mar 2017 09:35:27 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache/2.4.25 (Win64) PHP/5.6.30
X-Powered-By:PHP/5.6.30
Request Headers
view source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive
Content-Length:35
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:PHPSESSID=t2n11rnpt1mbgr8f0fiim0rkb3
Host:localhost:8078
Origin:http://localhost:8078
Referer:http://localhost:8078/byse/login.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
X-Requested-With:XMLHttpRequest
Query String Parameters
view source
view URL encoded
action:login
Form Data
view source
view URL encoded
usernum:13551101020
password:123456
Name
0 / 6 requests ❘ 0 B / 1.1 KB transferred

Console
Quick source





Preserve log
︿ 0
先确定有没有报错,http返回了200,PHP应该是没问题了,就看看前端的js有没有报错,哪里报错了就行啦