ES6`fetch 未定义` [英] ES6 `fetch is undefined`
问题描述
我正在使用 ES6 和 Babel 构建一个站点.
I'm building a site with ES6 and Babel.
在脚本文件中,我需要对服务器上的服务进行 ajax 调用.为此,我这样做:
In a script file, I need to make an ajax call to a service on server. For that I'm doing like this:
fetch('url').then(
response => response.json()
).then(
supervisoryItems => doSomething(supervisoryItems)
)
在 Google Chrome 中这工作得很好,但它在 Firefox 或 IE 上不起作用(我收到错误 fetch is undefined
).在 Google 上搜索我发现这应该可以解决它:
In Google Chrome this works just fine, but it does not work on Firefox or IE (I'm getting the error fetch is undefined
).
Searching on Google I found this should fix it:
import promise from 'es6-promise'
promise.polyfill()
遗憾的是它没有改变任何东西,我有同样的问题.有什么帮助吗?
Sadly it does not change anything, I have the same issue. Any help?
推荐答案
您需要将 'isomorphic-fetch' 模块添加到您的 'package.json' 中,然后导入.
You need to add the 'isomorphic-fetch' module to your 'package.json' and then import this.
npm install --save isomorphic-fetch es6-promise
然后在你的代码中
import "isomorphic-fetch"
见https://www.npmjs.com/package/isomorphic-fetch
这篇关于ES6`fetch 未定义`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!