【问题标题】:Fetch polyfill not working on Edge (or IE)获取 polyfill 在 Edge(或 IE)上不起作用
【发布时间】:2018-12-18 09:24:27
【问题描述】:

我正在尝试使用 fetch polyfill“whatwg-fetch”发布请求(表单)。虽然这适用于 Chrome 和 FF,但它似乎不适用于 IE 和 Edge。

import 'whatwg-fetch';
const dataObject = {
    'a' : 'b'
}
const params = new URLSearchParams();
params.append('someData', JSON.stringify(dataObject));                         

fetch(url, {
  method: 'POST', 
  mode: 'cors', 
  cache: 'no-cache', 
  credentials: 'same-origin',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
  },
  redirect: 'follow',
  referrer: 'no-referrer', 
  body: params, 
  }).then((res) => {
    console.log(res);
  }).catch((e) => {
   console.log(e);
});

在 IE 和 Edge 中,此代码不发送请求并捕获异常:

e Failed to execute 'fetch()' on 'Window': Invalid argument. 

有什么想法吗?

【问题讨论】:

  • data 定义在哪里?
  • @apokryfos 谢谢,修复代码。
  • 好吧 URLSearchParams 在 IE 和 Edge 中不受支持。尝试使用query-string 包,看看它是否能解决问题。

标签: javascript ajax forms ecmascript-6 fetch-api


【解决方案1】:

您好 - 看来确实是加载问题。

你试试看

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.26.0/polyfill.min.js"></script>index.html 中检查它是否正常工作。

【讨论】:

  • 向页面添加新的脚本标签是不可能的,我尝试将 babel-polyfill 添加到 webapack 'entry' 并作为 JS 文件中的导入,但它仍然不起作用。
【解决方案2】:

我遇到了完全相同的问题,在我的情况下,我帮助添加了这样的标题和 .toString() ... headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}, body: form.toString()** ...

【讨论】:

    猜你喜欢
    • 2018-06-08
    • 2017-10-19
    • 2018-06-04
    • 2016-04-03
    • 2020-03-01
    • 1970-01-01
    • 2020-04-26
    • 2016-09-21
    • 2018-08-23
    相关资源
    最近更新 更多