How does Ajax perform cross-origin requests? Principle of Ajax cross-origin request, ajax

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.