2010-03-08 5 views
0

가끔씩 파싱 할 수없는 JSON을 반환하는 서버에 AJAX 호출을하고 있습니다. 서버가 내 통제하에 있지 않기 때문에 수정할 수 없습니다.JQuery JSON 호출로 JSON 구문 분석 오류를 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 
    }); 
} 

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

해석 할 수없는 JSON이 반환되면 updateForm 함수가 호출되지 않습니다.

클라이언트 측에서는 updateForm 함수의 마지막 줄을 호출하여 상태 표시 줄을 숨길 수 있습니까? AJAX 호출과 updateForm 둘 다에 try { } catch {} 절을 넣으려고했습니다.

답변

1

이 작업을 수행 할 수 있습니다 :

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 콜백 불을 성공 후, 그 성공 여부.

+0

니스. 나는 그 생각을 좋아한다. – Jrgns