我使用Aurelia Fetch Client库通过代码从后端服务器获取JSON数据:
struct MyNewHasableStruct {
let oneString: String
let oneInt: Int
}
extension MyNewHasableStruct : Hashable {
var hashValue : Int {
return oneString.hashValue ^ oneInt.hashValue
}
static func == (lhs: MyNewHasableStruct, rhs: MyNewHasableStruct) -> Bool {
return lhs.oneInt == rhs.oneInt && lhs.oneString == rhs.oneString
}
static func < (lhs: MyNewHasableStruct, rhs: MyNewHasableStruct) -> Bool {
return lhs.oneInt < rhs.oneInt && lhs.oneString < rhs.oneString
}
}
通过代码从另一个代码调用metod getData() {
let httpClient = new HttpClient();
return httpClient.fetch('http://localhost:9220/get-data')
.then(response => response.json())
.then(data => return data);
}
}
:
getData()
正如您所看到的,我在这里使用dataService.getData().then(data => {
this.data = data;
}).catch(error => {
this.backendError = true;
});
语句,如果出现错误,我会调用它,但我也会在控制台中看到来自库的错误消息:“ vendor-bundle.js :1395未处理拒绝TypeError:无法获取“。我怎么能摆脱它?
答案 0 :(得分:2)
我不确定这是否是Aurelia HTTP Fetch Client的错误,但添加responseError
拦截器应删除控制台中的Unhandled Exception警告。
let http = new HttpClient();
http.configure(config => {
config.withInterceptor({
response(response) {
return response;
},
responseError(error) {
return error;
}
})
});
答案 1 :(得分:0)
此错误也可能来自.NET Core API中的query {
node(id: "$user") {
... on User {
pullRequests(first: 100) {
nodes {
reviewRequests(first: 100) {
nodes {
requestedReviewer {
... on User {
id
}
}
}
}
}
}
}
}
}
中间件。此中间件会从响应中删除所有创建CORS问题的标头,并导致您看到的“TypeError:Failed to fetch”错误。以下是我的解决方案示例,完整地描述了here。
UseDeveloperExceptionPage
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
{
var code = HttpStatusCode.InternalServerError;
var result = JsonConvert.SerializeObject(new { error = "An internal server error has occurred." });
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)code;
return context.Response.WriteAsync(result);
}