简体   繁体   中英

Ajax Post request not working

Hello I have created a script for login using ajax method I tried but this is not working for me getting unsuccessful can you please let me know why i am unable to work with this

function login() {
var username = document.getElementById('username').value;
var pass     = document.getElementById('pass').value;

if(username == '' && pass == '') {
    alert('Fields could not be left empty');
} else {
    $.ajax({
        type     : "POST",
        url      : "includes/register.php",
        data     : "command=login&username="+username+"&password="+pass,
        datatype : "json",
        success  :  function(data) {
            if (data.status == 200) {
                alert('Successfull');
                var return_data = data.responseText;
                document.getElementById('messagearea').innerHTML = return_data;
            } else {
                alert('Unsuccessful');
            }
        }
    });
}
}

it should run alert successful but I am getting alert('Unsuccessful') it means that status is not equal 200 can anyone help me out with that

Here is my php code

if(isset($_POST['command']) && $_POST['command'] == 'login') {
    $username = $_POST['username'];
    $pass     = $_POST['pass'];
    $password = md5($pass);

    $check  = mysqli_query($connection, "SELECT * FROM users WHERE username = '$username'");
    $result = mysqli_num_rows($check);
    if($result != 1) {
        echo "<div class='message'>Response Successful</div>";
    } else {
        echo "<div class='message'>Username/Password did not matched</div>";
    }
}

T in datatype should be capital,

$.ajax({
      type: "POST",
      url: "includes/register.php",
      data: "command=login&username=" + username + "&password=" + pass,
      dataType: "json",

As per the server code, your data should be look like this

data: "command=login&username=" + username + "&pass=" + pass,

change the code inside the success handler like this,

document.getElementById('messagearea').innerHTML = data;

If you want to check xhr request status you should use 3 parameter in success function .

success  :  function(data,text,xhr) {
            if (xhr.status == 200) {
                alert('Successfull');
                var return_data = data;
                document.getElementById('messagearea').innerHTML = return_data;
            } else {
                alert('Unsuccessful');
            }
        }

for dataType:"json"

if(isset($_POST['command']) && $_POST['command'] == 'login') {
    $username = $_POST['username'];
    $pass     = $_POST['pass'];
    $password = md5($pass);

    $check  = mysqli_query($connection, "SELECT * FROM users WHERE username = '$username'");
    $result = mysqli_num_rows($check);
    if($result != 1) {
        echo json_encode("<div class='message'>Response Successful</div>");
    } else {
        echo json_encode("<div class='message'>Username/Password did not matched</div>");
    }
}

You can try this solution:

$.ajax({
    type: "POST",
    url: "includes/register.php",
    data: {command: "login", username: username, password: pwd},
    dataType: "json",

Don't use "pass" into scripts jQuery, because sometimes it doesn't work properly.

Bye! :D

You need to add two more object in your ajax success function which will help you get more information of the request.

$.ajax({
    type     : "POST",
    url      : "register.php",
    data     : "command=login&username="+username+"&password="+pass,
    datatype : "json",
    success  :  function(data,xhr,req) {
        if (req.status == 200) {
            alert('Successfull');
            var return_data = data.responseText;
            document.getElementById('messagearea').innerHTML = return_data;
        } else {
            alert('Unsuccessful');
        }
    }
});

Also you need encode your return data into JSON so that your request return type match to JSON. If you closely look at your ajax request through Developer tool baar you will see that there are two more object return with your request containing different things.

  1. Contains Data
  2. Contains Request is success or failed.
  3. Contains different objects in array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM