Rocco The Taco
Rocco The Taco

Reputation: 3777

Error Handling for ajax/json fetch

This code snippet is part of a html 5 standalone web page. Can someone show me how to update this code to ask the person to check their internet connection if they cannot access the url? Right now it does nothing and that is not acceptable.

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

Upvotes: 0

Views: 104

Answers (2)

coolguy
coolguy

Reputation: 7954

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

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382132

The ajax function lets you precise an error callback ;

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

Upvotes: 1

Related Questions