2012-03-23 39 views
2

在下面的代碼中,是否可以從else塊調用錯誤函數?

$.getJSON('something.php', function(data) { 
    if (data.error === undefined) { 
    } else { 
     // How can I call the error function below? 
    } 
}).error(function() { 
    // This is the error function the be called. 
}); 

回答

7

只是單獨定義它,並把它稱爲:

$.getJSON('something.php', function(data) { 
    if (data.error === undefined) { 
    } else { 
     // How can I call the error function below? 
     handleError(); 
    } 
}).error(handleError); 

function handleError() { 
} 
+1

哈哈你是第二快於我。我正在寫同樣的東西。 – Miro 2012-03-23 17:36:31

相關問題