2012-09-03 105 views
0

此代碼片段是html 5獨立網頁的一部分。有人可以告訴我如何更新此代碼,要求該人檢查他們的互聯網連接,如果他們無法訪問該網址?現在它什麼都不做,這是不可接受的。處理ajax/json錯誤處理

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

回答

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