Ajax回调中的if语句

Hi im having a problem refreshing my page when a condition is met in php using ajax, if the php script is successful i want it to reload the page but when it is not then i only want to echo the errors.

The following is the part of my PHP code that returns the 'success' or error message:

if ($login) {
    echo "success";
} else {
    echo '<p>Username and Password does not match</p>';
};

This is working as i have tested it separately and the user gets logged in even though ajax

The problem i think comes in the ajax call back with the if statement. Here is the my js script that is running the ajax call back.

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

    var that = $(this),
        url = that.attr('action'),
        type = that.attr('method'),
        data = {};

    that.find('[name]').each(function(index, value){
        var that = $(this),
            name = that.attr('name'),
            value = that.val();

        data[name] = value;
    });

    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response){
            if(response == "success") {
                                //location.reload();
                window.location = "index.php";
            } else {
                $('#loginError').html(response);
            };
        }
    });

    return false;
});

I have tried everything on the net but every time the PHP script is successful it just echo's out the word success rather then redirecting the page or reloading

Thanks chris85 for the perfect advice worked like gold and was able to fix the problem, that was that there was a extra line at the top of my PHP script that I missed and saw it when i used console.log(response), also read the post you linked and it will help with future problems like this for me so thanks again.