Cross-origin AJAX Solution

Source: Internet
Author: User

Recently, the architecture group in the company is discussing AJAX in a heated manner. In the end, we will inevitably talk about how to cross-origin, because from the day when AJAX was born, the problem that XMLHttprequest objects cannot cross-origin requests in firefox persists. It is obviously unrealistic to wait for the browsers to solve this problem, smart Web developers have long thought of a series of methods to solve this problem. The following are two good methods:

1. intermediate layer transition mode:

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 PHP as an example. If you need to communicate with a php in different domains, the xmlhttprequest on the client first queries a PHP in the current domain, then, the PHP in the current domain communicates with PHP in different domains, and then the PHP in the current domain outputs response;

2. Use the <script> label

This method uses the src in the <script> tag to query a PHP 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:

<script LANGUAGE="Javascript" src="" id="get">
</script>
<script LANGUAGE="Javascript">
<!--
function get(url)
{
var obj = document.getElementById("get");
obj.src = url;
(obj.readStatus == 200)
{
alert(param);
}
}
function query()
{
get(get.php);
}
//-->
</script>
<BODY>
<INPUT TYPE="button" value="CLICK ME" onclick="query()">
</BODY>
</HTML>

The get. php code is:

<?php
echo "var param = 'www.achome.cn'";
?>

The final running result is that when you click the button, a dialog box with the content "www.achome.cn" will appear.

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.

I have seen many people reluctant to face up to the technical bottlenecks of ajax. In fact, AJAX should be more Ajax than AJAX, highlight the first A is to emphasize that AJAX is actually A method of asynchronous transmission, rather than the specific technology used in the end.

  1. Implement Ajax applications in MVC mode based on Dojo
  2. Use AJAX extenders to customize controls
  3. Introduction to the use of Ajax in ASP. Net

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.