0

Everything works fine with following program, except AJAX error fires:

javascript:

var data = {
    email: 'me@gmail.com',
    password: 'secretword'
};

$.ajax({
    type: "POST",
    dataType: "application/json",
    url: "http://localhost/CFBserver/validateUser.php",
    data: data,
    success: function (response) {
        console.log(response.responseText);
    },
    error: function (response) {
        console.log(response.responseText);
    }
});

}

php:

    <?php

    $conn = mysqli_connect('localhost', 'root', '', 'cfbdata');
    if (mysqli_connect_errno($conn)) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
    $password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);

    $sql = "SELECT * FROM users WHERE email = '$email' AND password = '$password'";

    if (!mysqli_query($conn, $sql)) {
        die('Error: ' . mysqli_error($conn));
    }

    $result = mysqli_query($conn, $sql);
    $numrows = mysqli_num_rows($result);
    if ($numrows > 0) {
        $message = array('result' => 'found',
            'email' => $email,
            'password' => $password,
        );
    } else {
        $message = array('result' => 'Not found',
            'email' => $email,
            'password' => $password,
        );
    }
    header('Content-type: application/json');
    echo json_encode($message);

    mysqli_close($conn);
    ?>

This is what console displays:

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        {"result":"found","email":"me@gmail.com","password":"secretword"}
    </body>
</html>

So php finds the record in the mysql database but on return to AJAX, error fires. Why is this?

Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94
dru37
  • 199
  • 1
  • 11
  • your open to SQL injection –  Dec 05 '16 at 01:49
  • your `error` and your `success` functions are identical so how do you know which is being trigged? –  Dec 05 '16 at 01:53
  • I do realize AJAX 'error' and 'success' fire the same code. But rest assured, I have confirmed 'error' is firing. Also, this is test code so I'm not concerned with security vulnerabilities at this stage of development. – dru37 Dec 05 '16 at 01:54
  • Status 200 is not an error. That means success – TheValyreanGroup Dec 05 '16 at 01:57
  • Out of curiosity, what happens if you change `dataType` to just `JSON`? Also, how do you know it's your error method firing when they both do exactly the same thing... – Darren Dec 05 '16 at 02:12
  • Changing dataType to json doesn't have any effect. – dru37 Dec 05 '16 at 02:20
  • how about removing the `header('Content-type: application/json');` – Veshraj Joshi Dec 05 '16 at 04:06

1 Answers1

0

Your AJAX is expecting a JSON response, but is getting HTML. That's why the request returns status code 200 (= OK), but your JS won't work.

PHP's json_encode doesn't add HTML by itself, so you're probably outputting to a template (or you've wrapped your PHP in HTML).

As others have also mentioned, you're open to SQL injection. There is also no way to be sure your error method is firing, since both your AJAX' error and success do the same thing.

Community
  • 1
  • 1