ES6 ' fetch is undefined`


Estoy construyendo un sitio con ES6 y Babel.

En un archivo de script, necesito hacer una llamada ajax a un servicio en el servidor. Para eso estoy haciendo así:

fetch('url').then(
    response => response.json()
).then(
    supervisoryItems => doSomething(supervisoryItems)
)

En Google Chrome esto funciona bien, pero no funciona en Firefox o IE (estoy recibiendo el error fetch is undefined). Buscando en Google Encontré que esto debería arreglarlo:

import promise from 'es6-promise'
promise.polyfill()

Lamentablemente no cambia nada, tengo el mismo problema. Alguna ayuda?

Author: JJJ, 2016-04-07

4 answers

Necesita agregar el módulo 'isomorphic-fetch' a su paquete'.json ' y luego importar esto.

npm install --save isomorphic-fetch es6-promise

Luego en su código

import "isomorphic-fetch"

Véase https://www.npmjs.com/package/isomorphic-fetch

 50
Author: Kieran Johnson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-09 11:11:24

Usaré los dos siguientes cdn de esta manera:

<script src="//cdn.jsdelivr.net/bluebird/3.5.0/bluebird.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.js"></script>
 5
Author: Thomas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-04 09:07:14

Babel-polyfill ( http://babeljs.io/#polyfill ) actualmente no incluye fetch en el polyfill. Estaba pensando en añadirlo.

Pero sí puede usar https://github.com/github/fetch

 3
Author: hzoo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-27 12:49:58

Acabo de pasar por esto anoche. Al final, después de probar todo tipo de cosas, la solución fue bastante simple:

fetch('url').then(
response => response.json()
).then(
supervisoryItem => doSomething(supervisoryItem)
)

Se convirtió en

window.fetch('url').then(
response => response.json()
).then(
supervisoryItem => doSomething(supervisoryItem)
)

TL; DR fetch (stuff) debe ser window .fetch(cosas) EDITAR Esto funcionó para mí en Chrome

 0
Author: Salvatore,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-08 03:34:36