comebal
comebal

Reputation: 946

Jquery ajax data convert to string

I have a problem with my jquery ajax. I have this code:

$.ajax({
    url: '/users/validatepassword/'+current,
    success: function(data){
        status = data;
    },
    async: false
});

if(status == "Password correct")
{
    //do something
}

Basically, I want to capture the "data" that was returned on "success". But I cannot make a way for the if statement to work. I am think the "data" was not a string so I cannot make a comparison.

Upvotes: 6

Views: 31873

Answers (5)

GOK
GOK

Reputation: 2428

@Comebal: Buddy this is what u need to do:

Firstly remove the async:false

$.ajax({
    url: '/users/validatepassword/'+current,
    success: function(data){
       //alert(data); 
       if(data == "Password correct")
        {
                  //do something
        }
    }
});

Then, the major part is make sure data from ajax page is "Password correct" or else u can't "do something"... :)

Upvotes: 1

repsaj
repsaj

Reputation: 11

You see you should process the message within the success function not from outside.

var status = '';
$.ajax({
    url: '/users/validatepassword/'+current,
    async: false,
    success: function(data){
        status = data;
        if(status == "Password correct")
        {
            //do something
        }
    }
});

Upvotes: 1

mrsrinivas
mrsrinivas

Reputation: 35404

Define status outside ajax call. then access that everywhere.

var status = '';
$.ajax({
    url: '/users/validatepassword/'+current,
    async: false,
    dataType: "json",
    success: function(data){
        status = data;
    },

});

if(status == "Password correct")
{
    //do something
}

At users/validatepassword use json_encode()

echo json_encode("Password correct");

Upvotes: 6

Jayamurugan
Jayamurugan

Reputation: 1825

You can try like this,

 var response = $.ajax({
            url: '/users/validatepassword/'+current,
            async: false
        }).responseText;

        if(response == "Password correct")
        {
            //do something
        }

Upvotes: 2

Pradeeshnarayan
Pradeeshnarayan

Reputation: 1235

Try status checking condition inside the ajax code.

$.ajax({
    url: '/users/validatepassword/'+current,
    success: function(data){
        status = data;
        if(status == "Password correct")
        {
         //do something
        }
    },
    async: false
});

if the condition is outside ajax, It will execute before the ajax return.

Upvotes: 4

Related Questions