首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我在使用Internet Explorer 11的Fetch API时遇到问题

我在使用Internet Explorer 11的Fetch API时遇到问题
EN

Stack Overflow用户
提问于 2018-09-28 17:40:32
回答 3查看 9.4K关注 0票数 3

我对这段代码有一些问题。我将使用此代码而不是polyfill。代码可以使用Firefox、Chrome、Opera和MS Edge运行,但Internet Explorer 11没有响应。有没有人能给我一个排除故障的提示。

function fetchFile(file,method,params,target) {

const controller = new AbortController();
const signal = controller.signal;

setTimeout(controller.abort.bind(controller), 5000); 

var options = { method: method, signal }; 

if (params != 'default') {

switch (method) { case 'GET':  file = file+'?'+params; break;

                  case 'POST': var data = params; break; default: data = 'data'; break;}

} 

switch (method) { case 'POST': options.body = data; options.headers = {"Content-Type": "application/x-www-form-urlencoded"}; break;}

fetch(file, options).then(function (response)  {

  return response.text();

}).then(function (data)  {

  document.getElementById(target).innerHTML = data;  

  switch (data) {case '': loadXhttp(file,method,params,target); break;} 
  
  
}).catch(function (data) {


switch (error.name) {

   case 'AbortError': var error = 'Abort Error'; break; 

   default: error = 'Other Error'; break;} 

   document.write(error);

});

}

function loadXhttp(file,method,params,target) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById(target).innerHTML = this.responseText;
    }
  };
  xhttp.open(method, file, true);
  
  switch (method) {case 'GET': var send = null; break;
                  case 'POST': xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				               send = params; break;} 
  
  xhttp.send(send);
}
​

谢谢。

致以最好的问候,马库斯

EN

回答 3

Stack Overflow用户

发布于 2018-09-28 17:43:02

我建议你调查一下isomorphic fetch

应要求,以下是您将如何使用它

安装isomorphic-fetches6-promise

npm install --save isomorphic-fetch es6-promise

安装软件包后,在应用程序中对其执行require操作

require('es6-promise').polyfill()
require('isomorphic-fetch')

这应该允许您正常使用fetch

fetch(url).then(handleResponse).catch(handleError)
票数 3
EN

Stack Overflow用户

发布于 2019-01-15 17:56:42

我想在这里添加一些东西,因为我在IE11中使用fetch API时遇到了同样的问题。在我的例子中,我在vanilla JS中使用的fetch没有与任何框架结合使用。

我已经有了一个来自here的polyfill。只需下载fetch.umd.js文件并放入脚本标记即可。然后我得到了未定义的Promise错误。所以我也不得不从here得到一个Promise polyfill。

就在我使用Visual Studio加载其他脚本之前,我在布局文件中的一个简单脚本标记中使用了这两个脚本,Promise polyfill也有cdn:

<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>

我希望它能帮助那些寻找解决方案的人,而不必像上面的例子中提到的那样安装它们。

票数 2
EN

Stack Overflow用户

发布于 2018-09-28 17:44:53

我使用过同构抓取,它在所有浏览器中都工作得很好。

require('es6-promise').polyfill();
require('isomorphic-fetch');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52552498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档