【问题标题】:Getting fetch is undefined in internet explorer 11?在 Internet Explorer 11 中获取未定义?
【发布时间】:2019-01-11 02:58:39
【问题描述】:

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

这是我的代码:

"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();
            });
        };
    });


});

【问题讨论】:

  • 我建议你只使用 jQuery $.ajax() 函数
  • 嘿@Phil,正如我在上面的代码中所述。我使用了$.ajax,但由于某种原因它不起作用,它给了我一个res.test() 未找到的错误。如果您说$.ajax 可以使用我的代码。你能告诉我一个工作代码吗?谢谢。

标签: javascript jquery internet-explorer-11


【解决方案1】:

尽管如果您已经在使用 jQuery,那么使用 jQuery 的内置请求函数(如 $.get)会很有意义,如果您想在不更改代码的情况下使用 fetch,一种选择是简单地包含一个 polyfill for fetch:添加以下内容

<script src="https://cdn.polyfill.io/v2/polyfill.js?features=fetch"></script>

(或其他一些包含 Fetch 的 polyfill 脚本)到您的 HTML。

【讨论】:

  • 嘿@CertainPerformance 感谢您的回答。但是,我有一个问题要问你:那么,如果我将代码的开头放在我的 html 的标题中,并运行我的代码,我的网络应用程序是否可以在 internet explorer 11 中运行?
  • 如果当前脚本在其他浏览器中工作,那么是的,只需为fetch 添加一个polyfill 应该 导致脚本也可以在IE 中工作(如果fetch是您的脚本使用的唯一 ES6+ 功能)
  • 正确。好吧,让我试试,我会尽快让你。如果你的答案有效,你就知道你会得到什么。 :) 干杯。谢谢。
  • 最后一个问题:这也适用于 jquery 库 3.3.1 或更旧的 jquery 库吗?
  • 如果您当前脚本中的所有内容都可以在具有任何版本 jQuery 的其他浏览器上运行,那么如果 fetch 是您使用的唯一一个在 IE 中不存在的对象,那么 fetch无论 jQuery 版本如何,polyfill 都应该使脚本正常工作
【解决方案2】:

Internet Explorer 不支持fetch

有关哪些浏览器支持它的更多信息:https://caniuse.com/#feat=fetch

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    相关资源
    最近更新 更多