Using IFRAME to set agent to solve Ajax cross domain request problem _ajax related

Source: Internet
Author: User
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.
Related Article

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.