Apologies for any language barriers, I will do my best to explain clearly.
I created a function that generates a table from a JSON based on SQL query values within the JSON.
Sometimes, when the SQL query is incorrect, I encounter issues accessing the JSON. To handle this, I use a variable called test
which is defined within the $.getJSON
.
To address this issue, I use an if
statement to check if the test variable is defined or not. If it's not, I display an alert to notify the user :
if(typeof test == "undefined"){
$('#loading').hide();
alert("Error in the query, please adjust your filters."); //Error !
return;
}
The problem arises when even with a correct query, I still receive the error message. I'm unsure why this happens because when I print test
using console.log(test)
, it displays as 1 as intended...
Below is my complete function for reference :
function get_values(){
$('#loading').show();
$("table#mon_tableau").removeClass("hidden");
var myjson = "http://******/get_json_test.php?callback=?";
$('.filter').each(function(i, obj){
var input = $(obj).find("input")[0];
myjson += '&'+input.name+'=' + input.value;
});
$('.display').each(function(i,obj){
myjson += '&'+$(obj).attr('table')+'|'+$(obj).attr('titre')+'';
});
console.log(myjson);
$.getJSON(myjson, function(data){
var titre_colindex = 0;
var test = 1;
if(data.length != 0){
newRow = document.getElementById('mon_tableau').getElementsByTagName('tbody')[0].insertRow(-1);
$.each(data[0], function(index, valeur){
newCell = newRow.insertCell(titre_colindex);
newCell.innerHTML = index;
titre_colindex+=1;
});
$.each(data, function(i,ti){
var newRow;
var newCell;
newRow = document.getElementById('mon_tableau').getElementsByTagName('tbody')[0].insertRow(-1);
var i_colindex=0;
$.each(ti, function(index, valeur){
newCell = newRow.insertCell(i_colindex);
newCell.innerHTML = valeur;
i_colindex+=1;
});
});
console.log(erreur);
$('#loading').hide();
alert("There are " + data.length + " results");
}
else{
$('#loading').hide();
$("table#mon_tableau").addClass("hidden");
alert("There are no results...");
}
console.log(test);
});
if(typeof test == "undefined"){
$('#loading').hide();
alert("Error in the query, please adjust your filters.");
return;
}
console.log(test);
}