簡體   English   中英

如何使用匿名函數處理Ajax的$ get中的錯誤?

[英]How to handle error in ajax's $get with anonymous function?

我想寫一個像這樣的ajax函數

$.get("./v1/user/"+username, function(response) {
    document.getElementById("username").innerHTML=response.username;
});

在失敗時,我想更改其他元素的樣式屬性。

如何在此處編寫.fail或類似函數來處理錯誤?

如何在此處編寫.fail或類似函數來處理錯誤?

您可以使用jQuery deferred.fail()或3.0+版本的.fail().catch()

$.get("./v1/user/"+username)
.then(function(response) {
    document.getElementById("username").innerHTML=response.username;
})
.fail(function(jqxhr, textStatus, errorThrown) {
  // do stuff
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM