Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 929
comebal Avatar asked Jul 27 '12 08:07

comebal


4 Answers

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");
like image 153
mrsrinivas Avatar answered Nov 13 '22 08:11

mrsrinivas


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.

like image 34
Pradeeshnarayan Avatar answered Nov 13 '22 06:11

Pradeeshnarayan


You can try like this,

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

        if(response == "Password correct")
        {
            //do something
        }
like image 2
Jayamurugan Avatar answered Nov 13 '22 07:11

Jayamurugan


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
        }
    }
});
like image 1
repsaj Avatar answered Nov 13 '22 07:11

repsaj