Corn Hole LI
Corn Hole LI

Reputation: 63

ajax success function not redirecting on success

The ajax function below should redirect to a new page upon successful login but it only echoes the word "success" in the div that is for error messages. I have tried numerous variations to no avail, some of which I have included below. I have search this and all other sites and I know this is a problem for many but the suggested solutions for others has not worked for me, can anyone see what the problem is?

the ajax:

    function processLogin() {

        var username = $('#username').val();
        var password = $('#password').val();
        var link = $("mysite.com/newpage").attr('href');

        $('#bad_login').html('Checking Login Info...<img src="images/process.gif" />');
        $.ajax({
          type: "POST",
          url: "process_login.php",
          data: "username=" + username + "&password=" + password,
    success: function(data){
alert(data);
        if(data=="success"){  // **this is where the problem is***
             location.assign("http://www.mysite.com/newpage.php");
    exit;
            }else{
    window.setTimeout(function()
    {
    $('#bad_login').html(data);
    $('#data').css("display","block");
    $('#data').html(data);
    }, 2000);
        }
    }
       });
        return false;
       }

the partial php

if ($pass== $checkpass) {
$data="success";
echo $data;
} else {

// IF USER PASSWORD IS INCORRECT
echo "Incorrect password!";
}

have tried: window.location.href =

plus other variations I can not remember, losing my mind over this

Upvotes: 1

Views: 162

Answers (2)

jacouh
jacouh

Reputation: 8769

As the discussion on comments, you may have a UTF-8 bom char returned from process_login.php.

BOM Byte order mark is an invisible char, sometimes it's sent at the head of the document flux to indicate a UTF-8 document to the browser. You cannot see it by your alert().

Anyway, as you have Ajax call suceeded, so why not use a less strict checking:

if(data.indexOf("success") >= 0) {

in place of

if(data == "success") {

?

Upvotes: 1

Cgrain
Cgrain

Reputation: 149

Try to trim it: http://api.jquery.com/jQuery.trim/

data=$.trim(data); 
if(data=="success") { etc

At least, your Ajax is probably working correctly. As it's returning the correct thing in your Badlogin :D

Upvotes: 0

Related Questions