Ajax不断返回PHP的错误输出

this codes keeps returning 0 even if the username and password I entered is correct. I also did try the php codes without ajax but it works perfectly.

PHP

include('config/connection.php');

if(isset($_POST['login'])){
    $username = mysqli_real_escape_string($conn,$_POST['username']);
    $password = mysqli_real_escape_string($conn,$_POST['password']);

    $select_query = mysqli_query($conn,"SELECT * FROM tbl_users WHERE username='$username' AND password ='$password'");
    $count_rows = mysqli_num_rows($select_query);

    if($count_rows > 0){
        echo "1";
    } else {
        echo "0";
    }
}

AJAX

    $('form').on('submit', function (e) {

      e.preventDefault();

      $.ajax({
        type: 'post',
        url: 'phpscripts/function/fnc_login.php',
        data: $('form').serialize(),
        success: function (result) {
            //if fnc_login.php returned 1/true
            //redirect to index page
            if (result == 1) {                 
                window.location = "index.php";

            //if fnc_login.php returned 0/false
            } else {
                $('#message').html('User not found!').fadeIn('slow');
            }
        }
      });
    });
    return false;

you check integer value in response not string

if (result == "1") { }

Hope it solve your problem.

In your ajax success function, alert the result as so:

alert('-' + result + '-');

This will indicate the actual and complete response data received from the server. It's likely that your response contains a newline or perhaps some text that causes jquery to misunderstand the response datatype.

If the datatype appears wrong you can pass an explicit data type as so:

$.ajax({
    type: 'post',
    url: 'phpscripts/function/fnc_login.php',
    dataType: 'text',
    data: $('form').serialize(),

Finally, once you know what the response looks like and you know it's 100% correct, compare it as a string, like so:

if (result == "1") { }