Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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))       })     })   } } 
like image 517
zlwaterfield Avatar asked Mar 06 '16 17:03

zlwaterfield


2 Answers

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

like image 55
Dmitriy Avatar answered Sep 17 '22 16:09

Dmitriy


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

entry: {   app: ['whatwg-fetch', 'your-index.js'] } 
like image 26
Александр Янковский Avatar answered Sep 17 '22 16:09

Александр Янковский