Because the browser's same-origin policy Ajax requests are not able to receive the data returned by the request response
Request data requires invoking the browser's built-in constructor XMLHttpRequest () for instance object
var xhr = new XMLHttpRequest ();
Note the ActiveXObject ("Microsoft.XMLHTTP") supported prior to IE8; Remember to do compatibility processing Oh, I'm not going to write it here.
Get through this object
Get four states of data xhr.readystate This property holds several states of the requested data
1.xhr.open (Request method, request address, set up with/async);
2.xhr.send (null);//Send request if it is a POST request then the parameter needs to be passed in send
3. Will always detect whether the data is returned
4. Data returned so the value of Xhr.readystate is 4 indicates that the data returned successfully
So what do we need to do with cross-domain requests?
Programme one:
Using the SRC attribute of the script tag to request data Src property The requested data browser is not intercepted
<script srrc= "http://jiang.com/AJAX/data.php" ><script/>
So that we can request data, but there are some drawbacks.
1. We have no control when the page that sends the request is loaded here is the sending request
2. The way the request is requested is that a synchronous request needs to be requested to the data before the next code is executed so that the page load time may be extended
3. So it is recommended not to use such a way not recommended
Scenario Two:
Dynamically creating a script tag in JS specifying the requested interface
var script = document.createelement ("script");
Append script to head tag
Document.getelementstagname ("Head") [0].appendchild (script);
Advantage: We can control the timing of the request and the way the request is asynchronous does not prolong the page load time
Important: We can call a function in the request data page to receive the requested data using the function's formal parameter
Programme III:
Set a request header on the backend interface of the request
Access-control-allow-origin
Data that contains cross-domain requests from the front-end page of this request header will not be blocked by the browser.
Programme IV:
Use the <iframe> tag to place a page with a variable content part in this tab and send the request will be refreshed it's strictly not Ajax.
The way in which Ajax does not appear is to use the tag to implement the request data
Ajax request data across domains