This article introduces the sharing of cross-domain resources in JavaScript for your reference in detail. The specific content is as follows
1. Why is cross-domain resource sharing (CORS) proposed?
Because the security limitation of XHR implementing ajax is that XHR objects can only access resources in the same domain as the page containing it.
2. How to achieve cross-domain? (Cross browser)
// Create and return CORS object across browsers// param method : request method, get or post // param url : cross-domain requested url // return xhr : returned cross-domain resource object function createCORSRequest(method, url){ var xhr = new XMLHttpRequest(); if ("withCredentials" in xhr){ xhr.open(method, url, true); // CORS is all through asynchronous request} else if (typeof XDomainRequest != "undefined"){ // IE vxhr = new XDomainRequest(); xhr.open(method, url); } else { xhr = null; } return xhr; } var request = createCORSRequest("get", "http://localhost/aaa/dome2.php"); if (request){ // Used to replace onreadystatechange to detect successfully, indicating that data has been accepted request.onload = function(){ // Process the response information alert(request.responseText); // Get the response content}; // Used to replace onreadystatechange to detect errors. request.onerror = function(){ // Process the response information}; // Used to stop the ongoing request. request.onabort = function(){ // Process the response information alert(request.responseText); }; // Send request.send() across domains; }The above is all about this article, I hope it will be helpful to everyone's learning.