当我使用ajax发布数据时,我没有得到响应消息

时间:2018-05-02 06:55:10

标签: php jquery

1.我的PHP代码

<?php 

session_start();
include_once('connection.php'); 
include_once('function.php');
if(isset($_POST['username']) && isset($_POST['password'])) {

    $username = trim( $_POST['username'] );
    $password = trim( $_POST['password'] );

    if( empty($username) || empty($password)) {
        $res = ['error' => true, 'message' => 'All fields are required.'];
        echo json_encode($res);
    }else {  
        $user = login($conn, $username, $password);  
        if(count($user) > 0) {  
            $_SESSION['id'] = $user['id'];
            $_SESSION['name'] = $user['name'];
            $_SESSION['username'] = $user['username'];
            $res = ['error' => false, 'message' => 'User login successfully.'];
            echo json_encode($res);
        }else {
            $res = ['error' => true, 'message' => 'Username or password is incorrect.'];
            echo json_encode($res); 
        }
    }
}

2.我的html和脚本代码

<?php

include_once('inc/connection.php'); 
?> 
<!DOCTYPE html> 
    <html> 
        <head>
            <title>login</title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        </head> 
        <body>
            username:   <input type="text" name="uesr" id='uesrname' /><br />   
            password:   <input type="password" name="pass" id="password" /><br />   
            <button type="submit" name="submit" id="login">Login</button>

            <script type="text/javascript">     
                $(document).on('click','#login',function(){         
                    var username = $('#username').val();
                    var password =$('#password').val();         
                    if( username == '' || password == ''){
                        //alert('all fields are required');         
                    }else{          
                        $.ajax({
                            url:'inc/ajax.login.php',
                            method:'POST',
                            data:{ 
                                username:username, 
                                password:password
                            },
                            success:function($res){
                                console.log($res);
                            }           
                        });         
                    }   
                }); 
            </script>

    </body> </html>
  1. 列表项

2 个答案:

答案 0 :(得分:0)

使用dataType并从成功功能中删除$并尝试此

$.ajax({
                            url:'inc/ajax.login.php',
                            method:'POST',
                            dataType: 'json',
                            data:{ 
                                username:username, 
                                password:password
                            },
                            success:function(res){
                                console.log(res);
                            }           
                        });

答案 1 :(得分:0)

添加die();回显你的json_encode之后。 将您的ajax代码更改为:

$.ajax({
     url:'inc/ajax.login.php',
     method:'POST',
     dataType: 'json',
     data:{ 
        username:username, 
        password:password
     },
     success:function(res){
          console.log(res);
     }           
     }); 

如果您不使用dataType:'json',则可以通过以下方式访问“res”:

var res = JSON.parse(res);