fetch not defined in Safari (ReferenceError: Can't find variable: fetch)

fetch not defined in Safari (ReferenceError: Can't find variable: fetch)

由于某些原因,fetch (https://fetch.spec.whatwg.org/) 未在 Safari(版本 9.0.3)中定义,有人知道为什么吗?它似乎是标准并且在 Chrome 和 Firefox 中运行良好。似乎找不到其他人遇到同样的问题

我正在使用 react with redux,这里是一些示例代码:

export function fetchData (url) {
  return dispatch => {
    dispatch(loading())
    fetch(url, {
      method: 'GET'
    })
    .then(response => {
      response.json()
      .then(data => {
        dispatch(success(data))
      })
    })
  }
}

您可以对不支持的浏览器使用 https://github.com/github/fetch polyfill。

npm install whatwg-fetch --save; 

编辑:(根据评论)

添加

import 'whatwg-fetch'; 

在使用 fetch 之前的每个文件中 – oliviergg

使用 whatwg-fetch polyfill。 如果您使用 webpack,您可以将它添加到入口点

entry: {
  app: ['whatwg-fetch', 'your-index.js']
}