2010-03-08 31 views
0

我正在對有時返回無法解析的JSON的服務器進行AJAX調用。服務器不在我的控制之下,所以我無法解決這個問題。如何用JQuery JSON調用捕獲JSON分析錯誤?

function eventFunction(evt) { 
    $('div#status_bar').show(); 
    $.ajax({ 
     url: 'http://buggyserver.com/api/', 
     type: 'GET', 
     data: { 'mode': 'json', 'q': 'get/data' }, 
     dataType: 'json', 
     success: updateForm 
    }); 
} 

function updateForm(returned, status) { 
    if (status == 'success') { 
     //Update the form here 
    } 
    $('div#status_bar').hide(); 
} 

當返回不可解析的JSON時,updateForm函數不會被調用。

我該如何在客戶端確保調用updateForm函數的最後一行來隱藏狀態欄?我試過圍繞AJAX調用和updateForm放置try { } catch {}子句。成功後

function eventFunction(evt) { 
    $('div#status_bar').show(); 
    $.ajax({ 
     url: 'http://buggyserver.com/api/', 
     type: 'GET', 
     data: { 'mode': 'json', 'q': 'get/data' }, 
     dataType: 'json', 
     success: updateForm, 
     complete: function() { $('div#status_bar').hide(); } 
    }); 
} 

function updateForm(returned) { 
    //Update the form here 
} 

complete回調火災,無論是成功還是失敗:

回答

1

你能做到這一點。

+0

不錯。我喜歡這個主意。 – Jrgns 2010-03-08 19:16:30