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.