weixin_33674976 2017-08-06 07:16 采纳率: 0%
浏览 501

$ .ajax()不起作用[jquery]

Hey I've seen all the solutions here on StackOverflow on the topic but they seem to not work, I don't know why. I've this code inside the $(form).submit(function(e){...}); event handler.

        e.preventDefault();
        let formData = new FormData();
        formData.append('username', username.value);
        formData.append('password', password.value);
        
        $.ajax({
            url: 'login.php',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(response) {
                console.log(response);
            },
            error: function() {
                console.log('fail');
            }
        })

By the look of things, I think the login.php is receiving the formData value because when I submit the form nothing happens.

This is the login.php

<?php
    session_start();
    $password = $username = '';
    $_SESSION['user'] = '';


    if($_SERVER['REQUEST_METHOD'] == 'POST') {
        if(isset($_POST['login'])) {
            include_once('db.php');
            $username = strip_tags($_POST['username']);
            $passd = strip_tags($_POST['password']);

            $passd = md5($password);

            $sql = "select * from users where username = '" . $username . "' limit 1";
            $query = mysql_query($sql);

            if($query) {
                $row = mysql_fetch_assoc($query);
                $dbpass = $row['password'];
                if($password == $dbpass) {;
                    $_SESSION['user'] = $username;
                    header('Location: admin.php');
                } else {
                    echo 'Wrong username or password!';
                }
            } else {
                echo mysql_error();
            }
        }
    }
?>

How do I make this work? Thank you in advance.

</div>
  • 写回答

3条回答 默认 最新

  • weixin_33747129 2017-08-06 07:18
    关注

    remove

    let formData = new FormData();
    formData.append('username', username.value);
    formData.append('password', password.value);
    

    and replace

    data: formData,
    

    with :

    data: {username: username.value, password: password.value}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿