ajax/json获取的错误处理


Error Handling for ajax/json fetch

此代码片段是HTML5独立网页的一部分。有人能告诉我如何更新这个代码,让人们在无法访问url的情况下检查他们的互联网连接吗?现在它什么都不做,这是不可接受的。

//get grid for station
function insertTideGrid(marker, stationId, defaultCenter) {
    currentMarker = marker;
    currentStationId = stationId;
    if(grids['station'+stationId]) {
        addTextToInfowindow(marker, grids['station'+stationId], defaultCenter);
    } else {
        addTextToInfowindow(marker, '<img src="../images/ajax-loader.gif" /> <br /> Loading...', defaultCenter);
        $.ajax({
            url: 'http://www.mydomain/page.php',
            dataType: 'jsonp',
            type: 'GET',
            data: {'station': stationId, 'json': true},
            success: function(data){
            }
        })
    }
}
ajax函数使您能够精确地回调错误;
$.ajax({
        url: 'http://www.mydomain/page.php',
        dataType: 'jsonp',
        type: 'GET',
        data: {'station': stationId, 'json': true},
        success: function(data){
        },
        error: {
            alert('Please check your internet connection and reload the page');
        }
    })
//modify your ajax like this
 $.ajax({
            url: 'http://www.mydomain/page.php',
            dataType: 'jsonp',
            type: 'GET',
            data: {'station': stationId, 'json': true},
            success: function(data){
               // do you wat you want here..its OK
            },error:function(msg){
           alert(msg); //you will get the error here
           }
        });