Convert data in jquery ajax 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 think the “data” was not a string, so I cannot compare.

+5
source share
5 answers

Determine the status outside the ajax call. then access to it is everywhere.

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

});

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

In users / validatepassword use json_encode ()

echo json_encode("Password correct");
+6
source

Try condition checking condition inside 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 be executed before returning ajax.

+3

,

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

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

, .

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

@Comebal: , , :

First remove async: false

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

Then the main part is to make sure that the data from the ajax page is “Correctly correct”, otherwise you cannot “do something” ... :)

+1
source

All Articles