IE's Ajax cross-domain issue
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.
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
jQuery 中的 $.Ajax 数据类型
Publish Date:2024/03/24 Views:135 Category:JavaScript
-
本教程演示了在 jQuery 中使用 $.ajax 数据类型。
在 jQuery 中处理 $.ajax 失败
Publish Date:2024/03/24 Views:158 Category:JavaScript
-
在今天的文章中,我们将学习在 jQuery 中处理 AJAX 中的失败请求。
在 jQuery AJAX 中传递请求标头
Publish Date:2024/03/24 Views:142 Category:JavaScript
-
本教程演示了如何在 jQuery AJAX 中使用标头。
jQuery 中的 AJAX POST 请求
Publish Date:2024/03/24 Views:129 Category:JavaScript
-
在今天的文章中,我们将学习 jQuery 中的 AJAX 发布请求。
在 GoLang 中启用 CORS
Publish Date:2023/04/27 Views:181 Category:Go
-
本篇文章介绍如何在 GoLang 中启用和使用 CORS。Go 语言 CORS。跨源资源共享 (CORS) 是一个基于 HTTP 标头的过程,
Flask CORS 跨域问题
Publish Date:2023/03/27 Views:259 Category:Python
-
这个解释是关于我们在 Flask 应用程序中创建 API 时出现的一个问题,以及当我们尝试从不同域访问 Flask 应用程序时如何修复错误。
Node.js 中的 AJAX 调用
Publish Date:2023/03/27 Views:104 Category:Node.js
-
在本篇文章中,我们将了解 Node.js 中的 AJAX post 请求。