Why are there cross-domain issues:
The reason why Ajax needs "cross-domain" is the browser's homologous strategy. That is, the Ajax of a page can only get data from the same source or the same domain as the page.
How to call "homology" or "Same domain"? --the protocol, domain name, and port number must be the same. For example:
Http://google.com and https://google.com are different, because the agreement is different;
http://localhost:8080 and http://localhost:1000 are different because the ports are different;
http://localhost:8080 and https://google.com different, the agreement, domain name, port number are different, is not a family at all.
According to the same-origin strategy, I made a Web page http://localhost:8080/test.html can not directly get http://google.com data through Ajax.
Workaround One: JSONP: note: JSON can only send a get request, unable to send AJAX post request, with parameters passed, After the load request connection
Doem: If requesting www.33.com from www.34.com ,
A, local server write this
b, the other server to write this
Solution Two: Please out and front-end perfect PHP
DOEM2:
<script type= "Text/javascript" >functionShowweather () {//using AJAX to invoke weather information varXHR =NewXMLHttpRequest (); Xhr.onreadystatechange=function(){ if(xhr.readystate==4) {eval ("Var info=" +xhr.responsetext); vars = ""; S+ = "Address:" +info.weatherinfo.city+ "<br/>"; S+ = "Temperature:" +info.weatherinfo.temp+ "<br/>"; S+ = "Wind:" +info.weatherinfo.wd+ "<br/>"; document.getElementById (' Result '). InnerHTML =s; }} xhr.open (' Get ', './data.php '); Xhr.send (NULL); } window.onload=function() {showweather (); } </script>
<? PHP // request weather information across domains $url = "http://www.weather.com.cn/adat/sk/101010100.html"; // file_get_contents (File/url address);//url Address: Request the address and return information $cont file_get_contents ($url); Echo $cont;
Second, how to implement Ajax cross-domain