comebal comebal - 20 days ago 6
Ajax Question

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.

Answer

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");