Second, how to implement Ajax cross-domain

Source: Internet
Author: User

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

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.