Today you need to do remote data loading and rendering pages in your project until the development phase is aware of the problem of Ajax Cross-domain requests. So I want to solve this cross-domain problem with the way of proxy.
What is a cross domain?
simply, for security reasons, JavaScript on the page cannot access data on other servers, or "homology policies." Cross-domain is through some means to circumvent the same-origin policy restrictions, to achieve the effect of communication between different servers.
Scheme: Create a static proxy page on the server side, call this agent with IFRAME in the client, and then pass the document.getElementById ("proxy") of the IFRAME. Contentwindow object to request Ajax.
Server-side proxy page:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>ajax cross-Domain </title>
<script src= "Js/jquery-1.6.4.min.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
Document.domain = ' xxx.com ';
</script>
<body>
</body>
Client Ajax Invocation code example:
Copy Code code as follows:
<iframe src= "http://weixin.goumin.com/proxy.html" id= "proxy" style= display:none; "onload=" Loaddefaultdata (); " ></iframe>
<script type= "Text/javascript" >
Document.domain = "xxx.com";
function Loaddefaultdata () {
var iframe_jquery = document.getElementById ("proxy"). contentwindow.$;
Iframe_jquery.ajax ({
Type: "POST",
URL: "" +math.random (),
data:{},
Async:false,
DataType: ' JSON ',
Success:function (data) {
alert (data);
}
});
}
</script>
URL: "" +math.random (),
please pay attention to the red URL of this line, using a random function to solve the problem of browser caching. Make each request a different URL.