Solutions to Ajax Cross-domain problems (in PHP, for example)

Source: Internet
Author: User
Tags php code

Recently, the company's internal architecture group is talking about Ajax, and finally will inevitably talk about how to cross domain this problem, because from the day the Ajax was born, XMLHttpRequest objects in Firefox can not cross domain requests for the problem has been, waiting for browsers to solve this The problem is obviously unrealistic, and smart Web developers have long thought out a series of ways to solve the problem, and here are two good ways to do it:

1. How to use the middle-tier transition:

Intermediate transition, it is obvious that in the Ajax and different domains of the server communication between the middle of the transition, this layer of transition can be PHP, JSP, C + +, and any other network communication functions of the language, by the middle tier to different domains of the server to read data operations. Take PHP To do an example, if you need to a different domain of a PHP to communicate, now the client's XMLHttpRequest first query the domain of a PHP, and then by this domain PHP to and different domains of PHP to communicate, and then by the domain of PHP output response;

2. Use <script> label

This method is to use the SRC in <script> tag to query a PHP to get response, because the SRC attribute of the <script> tag does not have cross-domain problems.

For example, let's take a closer look:

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

Where the get.php code is:

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

The final run result is that when you click on that button, it appears with a dialog box that reads "www.achome.cn."

This method, also known as Ajaj or Ajax without XMLHttpRequest, replaces X with J because of the use of the <script> tag instead of XML and XMLHttpRequest.

How, very simple, I have seen many people do not want to face Ajax technology bottlenecks exist, in fact, Ajax should be Ajax rather than Ajax, highlighting the first A is to emphasize the fact that Ajax is developing an asynchronous transmission method, rather than exactly what kind of technology used.

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.