ChrisianBartram ChrisianBartram - 6 months ago 26
Ajax Question

Ajax string comparison

I have a basic ajax function

setInterval(function() {
var action = '';
var status = '';
$('#php-data').load('../Data/Dashboard.Data.php');
$.ajax({type: 'POST', url: 'Data/Dashboard.Image.php', data: { json: action }, dataType: 'json' }).done(function(data) {
console.log('Ajax Data: ' + data);
status = JSON.parse(data);
**if(status === 'false') {**
console.log("script is NOT running");
drawPieChart();
} else {
console.log("script is running");
$('#php-image').load('../Data/Dashboard.Image.php'); //todo only load this is the ajax returned from Dashboard.image.php is true
}
});
}, 5000);


basically the only thing my ajax returns is "false" so when I compare status to false it should be true and drawPieChart(); but instead it always runs the jquery load() function even though my ajax data is returning false every single time.
ive also tried this:

setInterval(function() {
var action = '';
var status = '';
$('#php-data').load('../Data/Dashboard.Data.php');
$.ajax({type: 'POST', url: 'Data/Dashboard.Image.php', data: { json: action }, dataType: 'json' }).done(function(data) {
console.log('Ajax Data: ' + data);
status = data;
**if(status === 'false') {**
console.log("script is NOT running");
drawPieChart();
} else {
console.log("script is running");
$('#php-image').load('../Data/Dashboard.Image.php'); //todo only load this is the ajax returned from Dashboard.image.php is true
}
});
}, 5000);


and this :

setInterval(function() {
var action = '';
$('#php-data').load('../Data/Dashboard.Data.php');
$.ajax({type: 'POST', url: 'Data/Dashboard.Image.php', data: { json: action }, dataType: 'json' }).done(function(data) {
console.log('Ajax Data: ' + data);
**if(data == 'false') {**

console.log("script is NOT running");
drawPieChart();
} else {
console.log("script is running");
$('#php-image').load('../Data/Dashboard.Image.php'); //todo only load this is the ajax returned from Dashboard.image.php is true
}
});
}, 5000);

Answer
if(status === 'false')

This suggests a string. use this instead:

if(status == false)

you could simplify it even further like so:

if(!status)
Comments