Javascript &引用;未经处理的拒绝”;Aurelia获取客户端出错

Javascript &引用;未经处理的拒绝”;Aurelia获取客户端出错,javascript,fetch,aurelia,Javascript,Fetch,Aurelia,我使用Aurelia Fetch Client library通过以下代码从后端服务器获取JSON数据: getData() { let httpClient = new HttpClient(); return httpClient.fetch('http://localhost:9220/get-data') .then(response => response.json()) .then(data => return data)

我使用Aurelia Fetch Client library通过以下代码从后端服务器获取JSON数据:

getData() {
    let httpClient = new HttpClient();

    return httpClient.fetch('http://localhost:9220/get-data')
        .then(response => response.json())
        .then(data => return data);
    }
}
dataService.getData().then(data => {
    this.data = data;
}).catch(error => {
    this.backendError = true;
});
代码从另一个代码调用metod
getData()

getData() {
    let httpClient = new HttpClient();

    return httpClient.fetch('http://localhost:9220/get-data')
        .then(response => response.json())
        .then(data => return data);
    }
}
dataService.getData().then(data => {
    this.data = data;
}).catch(error => {
    this.backendError = true;
});

正如您所看到的,我在这里使用了一个
catch
语句,如果调用了该语句,则会出错,但我在控制台中还看到一条来自库的错误消息:“vendor bundle.js:1395 Unhandled rejection TypeError:Failed to fetch”。如何消除它?

我不确定这是否是Aurelia HTTP Fetch客户端的错误,但是添加
responseError
拦截器应该会删除控制台中未处理的异常警告

let http = new HttpClient();

http.configure(config => {
    config.withInterceptor({
        response(response) {
            return response;
        },
        responseError(error) {
            return error;
        }
    })
});

此错误也可能来自.NET核心API中的
UsedeveloperCeptionPage
中间件。该中间件将从响应中删除所有头,这些头会创建CORS问题,并导致您看到的“TypeError:Failed to fetch”错误。下面是我的解决方案的一个示例,详细描述了它

.NET核心中间件 奥雷利亚拦截器
responseError(响应:任意):承诺{
if(响应实例of response){
返回response.json()。然后((serverError:serverError)=>{
//在这里处理错误。
返回承诺.拒绝(serverError.error);
}); 
}
}

由于您尝试调用的API失败,将导致此错误。是的,我知道,但有办法处理此错误吗?如果我的答案解决了您的查询,请将其标记为已接受。或者,如果没有,请告诉我,以便我可以相应地编辑它。