【问题标题】:how to fix 'fetch' is undefined in ie11?如何修复 'fetch' 在 ie11 中未定义?
【发布时间】:2019-01-10 22:00:08
【问题描述】:

我遇到了一个问题,我下面的代码在 Internet Explorer 11 中显示 'fetch' is undefined。我使用的是最新的 jquery,即 jquery-3.3.1.min.js,我什至尝试使用 $.ajax 而不是 fetch,但确实如此不行。谁能帮我解决这个问题,我的代码如下。它甚至可以在ie11中工作。非常感谢!

当我使用$_ajax 而不是fetch 时,我收到一个控制台错误消息res.text is not a function TypeError: res.text is not a function

这是我的代码:

"use strict";

$(function () {
    var myData = [];


    $.get("#{request.contextPath}/JobSearchItem.xhtml", function (data) {

        $("#searchTextField").autocomplete({
            minLength: 2,
            source: myData,
            select: function select(event, ui) {
                event.preventDefault();
                var url = '#{request.contextPath}/index.xhtml';
                var searchValue = ui.item.value;
                var data = new FormData();
                data.append('searchValue', searchValue);
                fetch(url, {
                    body: data,
                    method: "post"
                }).then(function (res) {
                    return res.text();
                }).then(function (text) {
                    $('#results').append($(text).find('#textTable'));
                    $('#results').append($(text).find('table'));
                    $('#results').append($(text).find('#bestTable'));
                    $("#clearone").show();
                });
            },
            response: function response(event, ui) {
                if (!ui.content.length) {
                    var message = { value: "", label: "NO SEARCH RESULT FOUND" };
                    ui.content.push(message);
                }
            }

        });


        $.each(data, function (k, v) {
            myData.push({
                id: v.id,
                label: v.label,
                value: v.id

            });
        });
    });


    $("#sJobClass").change(function () {
        var jobClassCd = $(this).val();
        if (jobClassCd !== 0) {
            var url = '#{request.contextPath}/index.xhtml';
            var searchValue = $('#sJobClass').val();
            var data = new FormData();
            data.append('searchValue', searchValue);
             fetch(url, {
                body: data,
                method: "post"
            }).then(function (res) {
                return res.text();
            }).then(function (text) {
                $('#results').append($(text).find('#textTable'));
                $('#results').append($(text).find('table'));
                $('#results').append($(text).find('#bestTable'));
                $("#clearone").show();
            });
        };
    });


});

【问题讨论】:

  • 使用 $.ajax() 或其他一些 jQuery 变体将是我会做的,它在 IE11 中确实有效。您说它“不起作用”,但您没有提供任何详细信息或尝试的代码,所以真的很难提供帮助。
  • ES6 `fetch is undefined`的可能重复
  • 所以,在 fetch(url, { body: data, method: "post" 中,我使用了 $.ajax 而不是 fetch
  • @Pointy 在我的尝试中查看更新的问题。
  • $.ajax() jQuery API 早于fetch() API,并且不是(也从未打算成为)一个插入式替代品。然而,这是一种从您的 JavaScript 代码设置和发出 HTTP 请求的方法,我假设这是您的基本目标。

标签: javascript jquery internet-explorer


【解决方案1】:

这样做-

npm install --save isomorphic-fetch es6-promise

在您的代码中导入并使用它。

【讨论】:

  • @AshsayMandwarya .. 所以我应该在命令提示符下还是在我的项目目录中安装?
  • 在你的系统中安装node,然后在你的项目目录中安装npm包。
  • 我做了,我应该像这样导入import "isomorphic-fetch" "use strict";,对吗?
  • "使用严格";导入“同构提取”;
  • 如果这是您正在寻找的答案,请将其标记为正确,以便其他人也可以获得帮助。
猜你喜欢
  • 2013-11-23
  • 1970-01-01
  • 2019-09-27
  • 1970-01-01
  • 2019-09-16
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多