迹忆客 EN >

当前位置:主页 > 学无止境 > 网络 >

IE对于Ajax跨域的问题

作者:迹忆 最近更新:2022/12/11 浏览次数:

Ajax在web系统中应用非常广泛,但是在web系统中经常会遇到跨域的问题。默认情况下,各浏览器对Ajax跨域访问是禁止的。限制尤其严格的当属IE浏览器。对于火狐,谷哥,safari等浏览器使用JQuery中的Ajax跨域访问是比较简单的。在《Ajax跨域Cookie相关设置》这篇文章中对这个问题所涉及到的Ajax和服务端进行了介绍。这里就不再赘述。

下面我们着重介绍在IE浏览器下应该如何允许Ajax跨域访问。

通过上面那篇文章我们知道,如果仅仅是允许浏览器跨域访问的话,那也是非常简单。在浏览器端不需要做任何的修改,就是正常的Ajax代码即可,所做的工作都在服务端进行设置。

Ajax代码

$.ajax({
       url: 'http://www.onmpw.com/ajax.php',
       type: 'get',
       dataType: 'json',
       success: function (res) {
           console.log(res);
       },
       error: function (err) {
          console.log(err);
       }
 }) 

php代码

$origin = $_SERVER['HTTP_ORIGIN'];
header("Access-Control-Allow-Origin:" . $origin);

当然了,对于IE浏览器来说其实服务端是相同的。所不同的就是前端部分。并不是IE浏览器都不支持类似上面的跨域方式,IE10+就是能够很好的支持的。这是值得我们高兴的地方。毕竟IE给我们留了一条活路。但是IE8、IE9依然活在大部分用户的电脑上面。所以我们也需要考虑那些执著的用户。

抱着侥幸的心理,我们在IE9上面运行了上述的代码。刚刚燃起的希望之火一下子就被浇灭了。怎么办,于是修改Ajax代码

$.ajax({
     url: 'http://www.onmpw.com/ajax.php',
     type: 'get',
     crossDomain: true,
     dataType: 'json',
     success: function (res) {
        console.log(res);
     },
     error: function (err) {
        console.log(err);
     }
 })

这次应该没问题了,继续运行。结果希望被二度消灭。经过一阵子的翻箱倒柜恍然大悟,原来IE9是不支持crossDomain的。

怎么办,难道只能放弃执著于IE9-的那些用户了?不能够,总得给那些用户一个坚持下去的理由啊!

虽然使用JQuery在IE9-下不能跨域访问,但是可以使用IE提供的XDomainRequest来进行访问。下面我们来看一下应该如何使用:

var xdr = new XDomainRequest(); // Use Microsoft XDR
xdr.open('get', 'http://www.onmpw.com/ajax.php');
xdr.onload = function () {
   var dom  = new ActiveXObject('Microsoft.XMLDOM'),JSON = $.parseJSON(xdr.responseText);
   dom.async = false;
   if (JSON == null || typeof (JSON) == 'undefined') {
         JSON = $.parseJSON(data.firstChild.textContent);
   }
   console.log(JSON.RES); //JSON.RES是返回结果
};
xdr.onerror = function() {
   _result = false;
};
xdr.send();

在服务端代码不用改变的情况下上述Ajax终于可以在IE9-浏览器上跨域跑起来了。

总起来说,IE对于跨域问题限制的还是比较严格的。对于我们程序员来说就比较麻烦了,但是这些问题总归要解决的。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

AJAX calls in Node.js

发布时间:2025/04/17 浏览次数:103 分类:Node.js

Representational State Transfer is abbreviated as REST . An API or Web API (Application Programming Interface) that complies with the restrictions and limitations of the REST architectural style and allows interaction with RESTful web servi

Enabling CORS in GoLang

发布时间:2025/04/15 浏览次数:103 分类:Go

This article describes how to enable and use CORS in GoLang. Go language CORS Cross-origin resource sharing (CORS) is a process based on HTTP headers that defines the origins from which browsers are allowed to load and use resources. CORS i

PHP with Ajax

发布时间:2025/04/13 浏览次数:140 分类:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Which technology do you choose to implement the web chat room?

发布时间:2025/03/18 浏览次数:62 分类:NETWORK

With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio

Implementing a group chat room using socket.io

发布时间:2025/03/18 浏览次数:68 分类:NETWORK

This article will share with you an example of using socket.io to realize the function of group chat. If you want to use socket.io, you must use nodejs to implement the server, so we need to install socket.io in nodejs Install socket.io How

Ajax cross-domain cookie related settings

发布时间:2025/03/18 浏览次数:89 分类:NETWORK

In web programming, we often encounter cross-domain issues. By default, browsers do not allow cross-domain access. Therefore, there is a concept here: CORS (Cross-Origin Resource Sharing). Before the HTML5 standard came out, CORS was not al

PHP+ajax to achieve cross-domain single sign-on

发布时间:2025/03/16 浏览次数:146 分类:NETWORK

We have previously introduced the principle of cross-domain single sign-on in "Detailed explanation of the implementation methods of three situations of SSO single sign-on" . Here we will introduce how to implement single sign-on using PHP

IE's Ajax cross-domain issue

发布时间:2025/03/16 浏览次数:191 分类:NETWORK

Ajax is widely used in web systems, but cross-domain issues are often encountered in web systems. By default, browsers prohibit Ajax cross-domain access. The IE browser has particularly strict restrictions. For browsers such as Firefox, Goo

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便