如何防止我的JSON Feed崩溃并在运行时忽略数据库中的错误(如果发生),以及如何将未定义的错误更改为字符串以作为回退(如“待处理”或“不可用”)返回。 “
我尝试输入Try,catch,finally方法。另外,我发现条件等于和为空的If语句将undefined更改为“ string”
var flight = document.getElementById("flight");
var time = document.getElementById("time");
var airline = document.getElementById("airline");
var stat = document.getElementById("status");
var cities = document.getElementById("cities");
//Loading the document
//this is for updating the url
var url = './airport-test.json';
//var url = './proxy.php';
$(document).ready(function () {
$.ajax({
type: "GET",
dataType: "json",
url: url,
async: true,
contentType: "application/json; charset=utf-8",
cache: false,
success: function (data) {
var flightString = "";
var timeString = "";
var airlineString = "";
var statString = "";
var citiesString = "";
if(data) {
try {
var a = JSON.parse(data);
console.log(a);
} catch(e) {
var errorMessage = e.name + '' + e.message;
console.log(errorMessage);
}
}
for (i= 0,l = data.length = 16; i < l; i++ ) {
code here I erased to fit this question better...
}
flight.insertAdjacentHTML('beforeend', flightString);
time.insertAdjacentHTML('beforeend', timeString);
airline.insertAdjacentHTML('beforeend', airlineString);
stat.insertAdjacentHTML('beforeend', statString);
cities.insertAdjacentHTML('beforeend', citiesString);
}});
});
我正在使用带有铬的树莓派,我只是希望即使在出现错误而不是空白页的情况下,JSON feed仍然可以显示。它在大多数时间都有效。另外,要将未定义的错误从错误更改为后备字符串。
答案 0 :(得分:0)
不能完全确定问题出在哪里,可以大胆猜测服务器未正确响应。
定义错误处理程序,
$.ajax({
type: "GET",
dataType: "json",
url: url,
async: true,
contentType: "application/json; charset=utf-8",
cache: false,
success: function (data) {
....
}
error: function (error) {
// handle your error here.
....
}