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

$ .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}
    
    评论

报告相同问题?