Question

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*/
    }
Was it helpful?

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,
    ..

OTHER TIPS

Could it be browser caching? Try adding this $.ajaxSetup({ cache: false });

You are using the wrong $.ajax option to retrieve the result. You should use the success option. Just change the

complete : function(xhr,data){

line for

success : function(data){

It should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top