douxian8883 2018-05-02 06:55
浏览 31

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

1.here is my php code

<?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.here is my html and script code

<?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. List item
  • 写回答

2条回答 默认 最新

  • dongzhankou2090 2018-05-02 07:07
    关注

    Use dataType and remove $ from success fucntion and try this

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?