Perfect solution to AJAX cross-origin problem _ javascript skills

Source: Internet
Author: User
I have seen that many people are reluctant to face up to the technical bottlenecks of ajax. In fact, AJAX should be more Ajax than AJAX, highlight first A: I want to emphasize that AJAX is actually A method of asynchronous transmission, rather than the specific technology used since the birth of AJAX, the XMLHttprequest object does not support cross-origin requests. This seems to be a classic problem. This is caused by the same-source javascript policy (which is not discussed in depth here.

There are several solutions:

1. Intermediate Layer Transition (can be understood as "proxy "):

The intermediate transition, obviously, is to add a transitional layer between AJAX and servers in different domains for communication, this layer transition can be PHP, JSP, c ++, and other languages with network communication functions. The middle layer reads data to servers in different domains. Take as an example. If you need to communicate with an in different domains, the xmlhttprequest on the client first queries an in the current domain, then, the in the current domain will communicate with in different domains, and then the response output (response) in the current domain will be like this, I believe that the reader has clearly understood it.

2. Use the <script> label

This method uses the src in the <script> tag to query An aspx to obtain the response, because the src attribute of the <script> tag does not have a cross-origin issue.

Let's take a look at the following example:

The Code is as follows:

Cross-origin Ajax Problems

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: 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.