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.
- Implement Ajax applications in MVC mode based on Dojo
- Use AJAX extenders to customize controls
- Introduction to the use of Ajax in ASP. Net