首页 > 解决方案 > jquery .then 不会抛出错误

问题描述

使用 $.get promise (jquery 3.3.1) 不会显示任何错误,这使得调试变得不可能

$.get('/').then(function() {
    console.log(undef)
})

fetch('/').then(function() {
    console.log(undef)
})

第一个例子是静默的,没有抛出错误

第二个示例按预期抛出“Uncaught (in promise) ReferenceError: undef is not defined”

它是一个 jquery 错误还是我以错误的方式使用它?您如何建议使用 fetch 代替?我担心浏览器的兼容性

标签: javascriptjquerypromise

解决方案


您可以在链中添加失败功能:

$.get('/').then(function() {
    console.log(undef);
})
.fail(function(){
    console.log("Error");
})

参考


推荐阅读