Search code examples
phpjavascriptjqueryajaxajax-request

Jquery ajax request showing a old response


Hi I have a jQuery ajax request for a login system. At first, it worked very well. But after a few try, it just started to show negative response. I checked firebug and it says that the response is, in my case "Connected". But the ajax response just shows "Not_connected". I don't know what to do :(. Please help me.

This is my jquery code :

var data_str = "username="+usrn+"&password="+pwd;
$.ajax({
    type: "POST",
    url: "index.php?rnd=" + Math.random(),
    data : data_str,
    complete : function(xhr,data){
        if(data == 'connected'){window.location.href = 'admin.php';}
            else if(data = 'not_connected'){ error_gen.html('Invalid username or password'); }
            alert(data);
        }
});

AS for the PHP code :

$log_result = $u_obj->login_user();
if($log_result == true)/*user is connected*/
{
    echo 'connected';
    exit;/*stoping the script after sending the result*/
}
    elseif($log_result == false)/*error while logging in*/
    {
        echo 'not_connected';
        exit;/*stoping the script after sending the result*/
    }

Solution

  • Look at this thread: Is it possible to cache POST methods in HTTP?

    It might be that there are headers which now make browser caching the response (although it's POST).

    Also instead of rnd=" + Math.random() you can add write

    $.ajax({
        type: "POST",
        cache: false,
        ..