Analysis of Ajax cross-origin principle and implementation analysis in JQuery

Source: Internet
Author: User

Analysis of Ajax cross-origin principle and implementation analysis in JQuery
The emergence of AJAX allows the web page to be refreshed locally by performing a small amount of data exchange with the server in the background. However, for security reasons, ajax does not allow cross-origin communication. If you try to request data from different domains, an error will occur. If you can control the remote server where data resides and each request is sent to the same domain, you can avoid these security errors. However, what is the use of Web applications if they only stay on their own servers? What should I do if I need to collect data from multiple third-party servers? I. Thoughts On ajax cross-origin 1. Why can't Ajax cross-origin be used? Which part is stuck in? (Here we will draw a conclusion ). Ajax actually sends a GET or POST request to the server, obtains the server response result, and returns the client. Cross-origin errors occur when data is requested and cannot be retrieved. Therefore, the problem of cross-origin ajax can be converted to how data is retrieved. 2. Since we cannot directly access a third-party site, we can act as a proxy on the server and send a request to the proxy through ajax. After the proxy obtains the data, it returns it to the client. Of course, this is a solution, however, the response speed is a problem when a request is sent from the client to a third-party site through proxy and then the original response. 3. We found that we can place some js, css, and other files on third-party servers, such as CDN, to speed up the opening of webpages. This is no problem, that is to say, web pages can be loaded with js, css, images, and other resources on any site without being affected by "cross-origin. At this time, we will think: since we can call the js of a third-party site, if we put the data into the js of a third-party site, can we not bring the data to the client? Next we will conduct an experiment to verify that our conjecture is not true: Open Visual Studio and create a new Web project. Here we use WebForm, add a js file named remoteJs in the project, write the following code: function GetRemoteData () {return "remote data" plugin, and enter the following code: copy the Code <! DOCTYPE html>

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.