24

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

我正在使用 redux 的反应,这里是一些示例代码:

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

2 回答 2

39

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

npm install whatwg-fetch --save; 

编辑:(根据评论)

添加

import 'whatwg-fetch'; 

在每个文件中使用 fetch – oliviergg

于 2016-03-06T17:52:15.350 回答
4

使用whatwg-fetchpolyfill。如果你使用webpack,你可以将它添加到入口点

entry: {
  app: ['whatwg-fetch', 'your-index.js']
}
于 2017-09-21T13:36:27.390 回答