我想写一个这样的ajax函数
$.get("./v1/user/"+username, function(response) {
document.getElementById("username").innerHTML=response.username;
});
如果失败,我想更改其他元素的样式属性。
如何在此处编写 .fail 或类似函数来处理错误?
最佳答案
How do I write a .fail or similar function here to handle the error?
您可以使用 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
})
关于javascript - 如何使用匿名函数处理ajax $get 中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39968937/