How does Ajax perform cross-origin requests? Principle of Ajax cross-origin request, ajax
The examples in this article share the specific implementation process of Ajax cross-origin requests for your reference. The specific content is as follows:
Next we will build two local sites to demonstrate
The first step is to build an Apache server locally ;;
Step 2: After the server is configured, configure two virtual domain names locally;
Step 3: Create a folder on drive C and name it "HTML5 ";
Step 4 find the configuration file of the Apache Virtual Host, and then open the configuration file
Step 5: Create a folder a and a folder B under the HTML5 folder created in step 3;
Step 6 modify the configuration file of the Apache Virtual Host,
Step 7 modify the host file and add the URLs a and B. Generally, the path of the host file is under C: \ Windows \ System32 \ drivers \ etc.
We create a 7.ajax.html file under the HTML5/a folder.
<! Doctype html>
Let's take a look at the requests in the same domain.
Create a PHP file under HTML5/a and return 'hello ';
At this time we open the current page http://www.a.com/7.ajax.html, click the button we found the request to the data;
Http://www.a.com/7.ajax.html
However, if the data you request is not in the same domain as the current file, a cross-origin request will be generated. In this case, access is prohibited.
For example, we now put the ajax. php file in the HTML5/a folder to the B folder.
After clicking this button, we will find that the request has an error, which means that the cross-origin request is restricted.
At this time, we need backend cooperation. You need to tell the backend to add an "Access-Control-Allow-Origin" header to the output.
For example, it means that the cross-domain request of this domain name is not affected by the cross-domain policy.
At this time, when I click, I can normally obtain cross-Origin data.
If you want to be compatible with IE
<! Doctype html>
For more information about IE, visit https://msdn.microsoft.com/en-us/library/cc288060 (VS.85). aspx
Ajax latest standards visit https://www.w3.org/TR/XMLHttpRequest2/
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.