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

梦想的初衷 提交于 2019-12-20 09:16:43

问题


For some reason fetch (https://fetch.spec.whatwg.org/) is not defined in Safari (Version 9.0.3), does anyone know why? It seems to be the standard and works fine in Chrome and Firefox. Can't seem to find anyone else having the same issue

I am using react with redux and here is some example code:

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

回答1:


You can use https://github.com/github/fetch polyfill for unsupported browsers.

npm install whatwg-fetch --save; 

Edit: (per the comments)

add

import 'whatwg-fetch'; 

in each file before using fetch – oliviergg




回答2:


Use whatwg-fetch polyfill. If you use webpack, you could just add it to entry point

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


来源:https://stackoverflow.com/questions/35830202/fetch-not-defined-in-safari-referenceerror-cant-find-variable-fetch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!