Summary of cross-domain solutions

1 Cross domain via jsonp
        1.) Native implementation:
         <script>
            var script = document.createElement('script');
            script.type = 'text/javascript';
        
            // Pass parameters and specify the callback execution function as onBack
            script.src = 'http://www.....:8080/login?user=admin&callback=onBack';
            document.head.appendChild(script);
        
            // callback execution function
            function onBack(res) {
                alert(JSON.stringify(res));
            }
         </script>
    2. document.domain + iframe cross-domain  
        This solution is limited to cross-domain application scenarios with the same main domain and different subdomains.
        1.) Parent window: (http://www.domain.com/a.html)

            <iframe id="iframe" src="http://child.domain.com/b.html"></iframe>
            <script>
                document.domain = 'domain.com';
                var user = 'admin';
            </script>
            2.) Child window: (http://child.domain.com/b.html)
            
            <script>
                document.domain = 'domain.com';
                // Get the variable in the parent window
                alert('get js data from parent ---> ' + window.parent.user);
            </script>

        Disadvantages: See below for rendering and loading optimization

    3, nginx proxy cross-domain
    4. Nodejs middleware proxy cross-domain
    5. The backend sets the security domain name in the header information
    
    For more cross-domain specific content, please see https://segmentfault.com/a/1190000011145364


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325585011&siteId=291194637