AJAX 请求在 IE8 和 IE9 中不工作
AJAX request not working in IE8 and IE9
此 AJAX 代码在 IE10+ 和 Chrome 以及其他浏览器中有效,但在 IE8 和 IE9 中无效。
<table id="table" border="1">
<tbody style="display: table-row-group"></tbody>
</table>
jQuery(document).ready(function(){
$.support.cors = true;
var url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22EURUSD%22%2C%20%22CADUSD%22%2C%20%22GBPUSD%22%2C%20%22AEDUSD%22%2C%20%22TRYUSD%22%2C%20%22RUBUSD%22%2C%20%22INRUSD%22%2C%20%22SARUSD%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
var $tbody = $('#table').find('tbody');
var $thead = $('#table').find('thead');
$.ajaxSetup({ cache: false });
$.ajax({
crossDomain: true,
type: "GET",
url: url,
cache: false
}).done(function (data) {
alert("lev 2 ");
var ObjectKeys = Object.keys(data.query.results.rate[0]);
var row = "<tr>";
row += "</tr>";
$thead.append(row);
$.each(data.query.results.rate, function (i, el) {
console.log("lev 3 = " + i);
$tbody.append($('<tr />').append($('<td />').text(el.id)).append($('<td />').text(el.Name)).append($('<td />').text(el.Rate)).append($('<td />').text(el.Ask)).append($('<td />').text(el.Bid)));
});
});
});
我该如何解决这个问题?
问题是 IE8 不支持 Cross Origin Resource Sharing (CORS) XHR,因此您无法使用原生 XHR 或 jQuery 的 $. ajax.
对于IE8,微软决定拿出自己的跨域XHR,而不是使用CORS XHR,称为XDomainRequest, so you'll have to implement that to support IE8 users. An example usage can be found in this answer。
或者,您可以通过本地服务器端代理跨域请求,使外部请求成为服务器到服务器的情况,不受Same Origin Policy.
此 AJAX 代码在 IE10+ 和 Chrome 以及其他浏览器中有效,但在 IE8 和 IE9 中无效。
<table id="table" border="1">
<tbody style="display: table-row-group"></tbody>
</table>
jQuery(document).ready(function(){
$.support.cors = true;
var url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22EURUSD%22%2C%20%22CADUSD%22%2C%20%22GBPUSD%22%2C%20%22AEDUSD%22%2C%20%22TRYUSD%22%2C%20%22RUBUSD%22%2C%20%22INRUSD%22%2C%20%22SARUSD%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
var $tbody = $('#table').find('tbody');
var $thead = $('#table').find('thead');
$.ajaxSetup({ cache: false });
$.ajax({
crossDomain: true,
type: "GET",
url: url,
cache: false
}).done(function (data) {
alert("lev 2 ");
var ObjectKeys = Object.keys(data.query.results.rate[0]);
var row = "<tr>";
row += "</tr>";
$thead.append(row);
$.each(data.query.results.rate, function (i, el) {
console.log("lev 3 = " + i);
$tbody.append($('<tr />').append($('<td />').text(el.id)).append($('<td />').text(el.Name)).append($('<td />').text(el.Rate)).append($('<td />').text(el.Ask)).append($('<td />').text(el.Bid)));
});
});
});
我该如何解决这个问题?
问题是 IE8 不支持 Cross Origin Resource Sharing (CORS) XHR,因此您无法使用原生 XHR 或 jQuery 的 $. ajax.
对于IE8,微软决定拿出自己的跨域XHR,而不是使用CORS XHR,称为XDomainRequest, so you'll have to implement that to support IE8 users. An example usage can be found in this answer。
或者,您可以通过本地服务器端代理跨域请求,使外部请求成为服务器到服务器的情况,不受Same Origin Policy.