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
Detailed explanation of the implementation methods of SSO single sign-on in three
Publish Date:2025/03/18 Views:190 Category:NETWORK
-
Single Sign On (SSO) is not unfamiliar to us. For large systems, using SSO can reduce a lot of trouble for users. Take Baidu for example. Baidu has many subsystems - Baidu Experience, Baidu Knows, Baidu Library, etc. If we need to enter a u
Which technology do you choose to implement the web chat room?
Publish Date:2025/03/18 Views:61 Category: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
Publish Date:2025/03/18 Views:65 Category: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
Publish Date:2025/03/18 Views:87 Category: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
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
How to fix cross-origin (CORS) issues in Angular
Publish Date:2025/03/17 Views:192 Category:NETWORK
-
When we use a backend server for our Angular application during development, when trying to request resources from the API, we may encounter some cross-origin (CORS) restrictions that prevent us from accessing data from the API.
How to avoid cross-origin (CORS) issues in React/Next.js
Publish Date:2025/03/17 Views:166 Category:NETWORK
-
In this article, we will introduce how to avoid cross-origin (CORS) issues in React/Next.js. Cross-origin resource sharing (CORS) is a protocol that defines how web requests should be handled when crossing different URLs.
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 中的失败请求。