【发布时间】: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