Javascript 如何处理ajax中的错误';s$get与匿名函数?

Javascript 如何处理ajax中的错误';s$get与匿名函数?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我想写一个像这样的ajax函数 $.get("./v1/user/"+username, function(response) { document.getElementById("username").innerHTML=response.username; }); 失败时,我想更改其他元素的样式属性 如何在此处编写.fail或类似函数来处理错误 如何在此处编写.fail或类似函数来处理错误 您可以使用jQuery或版本3.0+.fail()或 如何在此处编写.fail或类似函数来处

我想写一个像这样的ajax函数

$.get("./v1/user/"+username, function(response) {
    document.getElementById("username").innerHTML=response.username;
});
失败时,我想更改其他元素的样式属性

如何在此处编写.fail或类似函数来处理错误

如何在此处编写.fail或类似函数来处理错误

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

如何在此处编写.fail或类似函数来处理错误

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

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