【问题标题】:fetch not defined in Safari (ReferenceError: Can't find variable: fetch)在 Safari 中未定义获取(ReferenceError:找不到变量:获取)
【发布时间】:2016-06-20 04:53:58
【问题描述】:

由于某种原因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))
      })
    })
  }
}

【问题讨论】:

标签: javascript safari fetch-api


【解决方案1】:

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

npm install whatwg-fetch --save; 

编辑:(根据 cmets)

添加

import 'whatwg-fetch'; 

在每个文件中使用 fetch – oliviergg

【讨论】:

  • 谢谢,应该知道的。
  • 我正在使用它(es6-promise polyfill 也是如此),但在 iOS 上查看它时,我一直收到完全相同的错误。
  • (没有可用于 cmets 的编辑..):Stock Android 也有同样的问题。
  • @nickvda 在使用 fetch 之前尝试添加 import 'whatwg-fetch';
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2018-10-26
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 2013-05-27
    相关资源
    最近更新 更多