【发布时间】: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 中确实有效。您说它“不起作用”,但您没有提供任何详细信息或尝试的代码,所以真的很难提供帮助。 -
所以,在
fetch(url, { body: data, method: "post"中,我使用了$.ajax而不是fetch -
@Pointy 在我的尝试中查看更新的问题。
-
$.ajax()jQuery API 早于fetch()API,并且不是(也从未打算成为)一个插入式替代品。然而,这是一种从您的 JavaScript 代码设置和发出 HTTP 请求的方法,我假设这是您的基本目标。
标签: javascript jquery internet-explorer