ajax / json fetch的错误处理

时间:2012-09-03 09:29:00

标签: php jquery ajax json

此代码段是html 5独立网页的一部分。有人可以告诉我如何更新此代码,以要求该人检查他们的互联网连接,如果他们无法访问该网址?现在它什么都不做,这是不可接受的。

//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){
            }
        })
    }
}

2 个答案:

答案 0 :(得分:1)

ajax function可让您精确地回复错误;

$.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');
        }
    })

答案 1 :(得分:1)

//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
           }
        });