jQuery.Ajax IE8 无效(CORS)

今天在开发的时候,遇到一个问题,$.get()在 IE8 浏览器不起作用,但 Chrome,Firefox 却是可以的,网上资料很多,最后发现是 IE8 默认不支持 CORS 请求,需要手动开启下:

jQuery.support.cors = true;

//url 是跨域的地址
$.get(url, , function (data) {
    //...
});

参考资料:Ajax call not working in IE8


后来发现上面的设置在 IE8/IE9 中无效,需要另外的解决方案:jQuery-ajaxTransport-XDomainRequest

示例写法:

$.getJSON('http://jsonmoon.jsapp.us/').done(function(data) {
  console.log(data.name.first);
});

// POST
$.ajax({
  url: 'http://frozen-woodland-5503.herokuapp.com/cors.json',
  data: 'this is data being posted to the server',
  contentType: 'text/plain',
  type: 'POST',
  dataType: 'json'
}).done(function(data) {
  console.log(data.name.last);
});

参考资料:IE8、9 下的资源跨域请求

原文地址:https://www.cnblogs.com/xishuai/p/jquery-ajax-ie8-cors.html