Why AbortController is not defined?

时间:2019-04-17 01:46:13

标签: javascript fetch

I want to be able to cancel requests with fetch API and use new AbortController() Unfortunately I get an error in the console: AbortController is not defined

//  this.aborter = new XMLHttpRequest()  no error
    this.aborter = new AbortController() error

What might be the reason? I'm using just vanilla JS without any dependencies.

2 个答案:

答案 0 :(得分:0)

AbortController上的MDN文档包含受支持的浏览器的最新表格。

尽管目前的浏览器应很好地支持该API,但该API仍被标记为实验性的。 Firefox自2017年11月(FF 57)开始使用,Chrome随后是2018年4月(Chrome 66)。

答案 1 :(得分:0)

尝试

this.aborter = new window.AbortController();

我发现在Chrome(v77)上,通过将其指定为window属性无法识别AbortController。

此外,致电

this.aborter.abort()

您可能需要重新初始化

this.aborter = new window.AbortController();

或将来的提取语句将不起作用(状态将被中止并且将引发错误!)。