Отображать сообщение об ошибке, когда результат ajax возвращает нулевое значение

Я хотел бы спросить, как отобразить сообщение об ошибке, когда нулевое значение и значение 0 в AJAX результат из SQL

{"value":
{"columns": [["More than 85%",null],["Less than 85%",0]],
"type":"pie"}
}

иначе не показывается всплывающее сообщение.

$.ajax({
type: "POST",
url: "charts/prod.php?year=" + $("#selectyear").val() + "&month=" + $("#selectmonth").val(),
dataType: "json",
success: function (result) {
var chart = c3.generate({
bindto: '#piepie',
data: result.value,
color: {
pattern: ['#f35213', '#f1af4c']
},
pie: { title: "Productivity", }
});
},
error: function() {
if ((result == null) && (result == 0)){
alert ('Data are not ready yet!!');
}
else {
('error');
}
}
});

1

Решение

Переменная result не существует в error: функция. Вы должны сделать этот тест в success: функция.

null а также 0 значения глубоко в структуре, вам нужно получить к ним доступ должным образом.

$.ajax({
type: "POST",
url: "charts/prod.php?year=" + $("#selectyear").val() + "&month=" + $("#selectmonth").val(),
dataType: "json",
success: function (result) {
if (result.value.columns[0][1] == null && result.value.columns[1][1] == 0) {
alert ('Data are not ready yet!!');
} else {
var chart = c3.generate({
bindto: '#piepie',
data: result.value,
color: {
pattern: ['#f35213', '#f1af4c']
},
pie: { title: "Productivity", }
});
}
},
error: function() {
alert('error');
}
});
1

Другие решения

Попробуй это,

$.ajax({
type: "POST",
url: "charts/prod.php?year=" + $("#selectyear").val() + "&month=" + $("#selectmonth").val(),
dataType: "json",
success: function (result) {
if ((result == null) && (result == 0)){
alert ('Data are not ready yet!!');
}else {
var chart = c3.generate({
bindto: '#piepie',
data: result.value,
color: {
pattern: ['#f35213', '#f1af4c'] },
pie: { title: "Productivity", }
});
}
},
error: function() {
alert('error');
}
});
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector