Cross-origin AJAX Solution

Source: Internet
Author: User

Cross-origin AJAX Solution

Since the birth of AJAX, the problem that XMLHttprequest objects cannot cross-origin requests has always existed. This seems to be a classic problem, due to the same-source javascript policy.

There are several solutions:

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

The intermediate transition adds a layer of transition between AJAX and servers in different domains. This layer of transition can be any language with network communication functions, such as PHP, JSP, and c ++, the middle layer reads data from servers in different domains. Take asp.net as an example. If you need to communicate with an asp.net in different domains, the xmlhttprequest on the client first queries an asp.net in the current domain, then, the asp.net of the local domain communicates with asp.net of different domains, and then the asp.net response output (response) of the local domain ).

 

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

Page code:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> GetDemo. aspx background code:

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace LearnJS {public partial class getDemo: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {Response. write ("var responseVal = 'dylan '");}}}View Code

This method is also called ajaj or ajax without xmlHttprequest. It replaces x with j Because xml and xmlHttprequest are not used when the <script> tag is used.

 

3. JQuery Solution

Page code:

<Html> The content in the remote server js.txt is:

Var Dylan = {name: "Dylan", email: Dylan@163.com}

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.