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.
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");
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.
You can try like this,
var response = $.ajax({
url: '/users/validatepassword/'+current,
async: false
}).responseText;
if(response == "Password correct")
{
//do something
}
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
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With