JIYIK CN >

Current Location:Home > Learning > NETWORK >

IE's Ajax cross-domain issue

Author:JIYIK Last Updated:2025/03/16 Views:

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, Google, and Safari, it is relatively simple to use Ajax in JQuery for cross-domain access. The article "Ajax Cross-Domain Cookie Settings" introduces Ajax and the server involved in this issue. I will not go into details here.

Below we will focus on how to allow Ajax cross-domain access in IE browser.

From the above article, we know that if we only allow the browser to access across domains, it is also very simple. No modification is required on the browser side, just the normal Ajax code, and all the work is set up on the server side.

Ajax Code

$.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 code

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

Of course, for IE browser, the server side is actually the same. The difference is the front-end part. It is not that IE browser does not support the cross-domain method above. IE10+ can support it very well. This is something we should be happy about. After all, IE has left us a way out. However, IE8 and IE9 are still alive on most users' computers. So we also need to consider those persistent users.

We ran the above code on IE9 with a fluke mentality. The hope that had just been ignited was extinguished in an instant. What should we do? So we modified the Ajax code

$.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);
     }
 })

This time it should be fine, so I continued running. But my hope was wiped out again. After a while of searching, I suddenly realized that IE9 does not support crossDomain.

What should I do? Should I just give up on those users who are obsessed with IE9-? No, I have to give those users a reason to stick with it!

Although JQuery cannot access cross-domain in IE9-, you can use XDomainRequest provided by IE to access it. Let's see how to use it:

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

The above Ajax can finally run cross-domain on IE9- browser without changing the server-side code.

In general, IE has relatively strict restrictions on cross-domain issues. It is more troublesome for us programmers, but these problems must be solved.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

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

Publish Date:2025/03/16 Views:145 Category: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

在 GoLang 中启用 CORS

Publish Date:2023/04/27 Views:181 Category:Go

本篇文章介绍如何在 GoLang 中启用和使用 CORS。Go 语言 CORS。跨源资源共享 (CORS) 是一个基于 HTTP 标头的过程,

带有 Ajax 的 PHP

Publish Date:2023/03/29 Views:143 Category:PHP

本文解释了如何将 PHP 与 Ajax 一起使用。

Flask CORS 跨域问题

Publish Date:2023/03/27 Views:259 Category:Python

这个解释是关于我们在 Flask 应用程序中创建 API 时出现的一个问题,以及当我们尝试从不同域访问 Flask 应用程序时如何修复错误。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial