最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is AbortController not defined? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Sep 24, 2022 at 4:03 starball 50.5k32 gold badges202 silver badges884 bronze badges asked Apr 17, 2019 at 1:46 karolis2017karolis2017 2,41510 gold badges27 silver badges54 bronze badges 5
  • 1 On which browser and which version? – Thum Choon Tat Commented Apr 17, 2019 at 1:50
  • I tried to create AborterController in a browser and same error. Chrome. Why it doesn't support new AbortController() ? – karolis2017 Commented Apr 17, 2019 at 1:54
  • 1 @karolis2017: Which version of Chrome – Ry- Commented Apr 17, 2019 at 1:55
  • 1 Based on MDN, AbortController is an experimental technology. You should be sure that you are running a browser with the correct version. See: developer.mozilla.org/en-US/docs/Web/API/AbortController – Abana Clara Commented Apr 17, 2019 at 2:07
  • updated browser and no problem. Thanks – karolis2017 Commented Apr 17, 2019 at 2:30
Add a comment  | 

2 Answers 2

Reset to default 8

try

this.aborter = new window.AbortController();

I found that on Chrome (v77) it didn't recognise AbortController with specifying it as a window property.

Also, after you call

this.aborter.abort()

you may need to re-initialise

this.aborter = new window.AbortController();

or future fetch statements won't work (the status will be aborted and it will throw an error!).

The MDN documentation on AbortController includes an up-to-date table of supported browsers.

The API is still marked as experimental, although it should be well supported in current browsers. Firefox has it since November 2017 (FF 57), and Chrome followed on April 2018 (Chrome 66).

发布评论

评论列表(0)

  1. 暂无评论