Aurelia HttpClient 取消请求
Aurelia HttpClient cancel requests
我正在尝试构建一个自动完成组件,并希望在用户键入时取消对服务器的未解决请求。
我在 HttpClient 的文档中找不到关于此的文档。它提到它是可取消的(与提取不同)但不是如何取消。 https://aurelia.io/docs/plugins/http-services
目前我有这个是我盲目拼凑的,不出所料它甚至没有中止请求:
async searchTermChanged(newValue, oldValue) {
if (newValue.length < 3)
return;
if (this.promises.length) {
this.promises.forEach(x => x.abort());
//should probably remove too
}
var promise = this.httpClient.post(this.endpoint, { SearchTerm: newValue });
this.promises.push(promise);
var data = await promise;
var response = JSON.parse(data.response);
this.results = response;
}
}
在哪里可以找到有关如何发出可取消请求的更多信息?我的 google-fu 让我失望了。
看起来你可以这样做:
this.client["pendingRequests"].forEach(request => {
request.abort();
});
我不得不这样做 ["pendingRequests"]
因为我使用的是 TypeScript 并且数组似乎不在定义范围内。
注意:我还在每个自动完成中使用一个范围限定的 HttpClient,这样当它取消所有以前的请求时,它不会意外取消应用程序正在请求的其他内容。
我正在尝试构建一个自动完成组件,并希望在用户键入时取消对服务器的未解决请求。
我在 HttpClient 的文档中找不到关于此的文档。它提到它是可取消的(与提取不同)但不是如何取消。 https://aurelia.io/docs/plugins/http-services
目前我有这个是我盲目拼凑的,不出所料它甚至没有中止请求:
async searchTermChanged(newValue, oldValue) {
if (newValue.length < 3)
return;
if (this.promises.length) {
this.promises.forEach(x => x.abort());
//should probably remove too
}
var promise = this.httpClient.post(this.endpoint, { SearchTerm: newValue });
this.promises.push(promise);
var data = await promise;
var response = JSON.parse(data.response);
this.results = response;
}
}
在哪里可以找到有关如何发出可取消请求的更多信息?我的 google-fu 让我失望了。
看起来你可以这样做:
this.client["pendingRequests"].forEach(request => {
request.abort();
});
我不得不这样做 ["pendingRequests"]
因为我使用的是 TypeScript 并且数组似乎不在定义范围内。
注意:我还在每个自动完成中使用一个范围限定的 HttpClient,这样当它取消所有以前的请求时,它不会意外取消应用程序正在请求的其他内容。