zoukankan      html  css  js  c++  java
  • 诡异的跨域问题

    Uncaught DOMException: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://127.0.0.1:8079" from accessing a cross-origin frame.

        at HTMLIFrameElement.<anonymous> (http://127.0.0.1:8079/static/oss/ajaxupload.3.9.js:573:33)

    使用AjaxUploadFile插件出错:代码

    var doc = iframe.contentDocument ? iframe.contentDocument : window.frames[iframe.id].document;

    一样的请求地址,域名和端口后都一致,还提示 accessing a cross-origin

    分析代码:

       /**
         * 上传单个文件到OSS
         * @param request
         * @param response
         * @throws Exception
         */
        public static void uploadFile(MultipartHttpServletRequest request, HttpServletResponse response) throws Exception {
            JSONObject json = new JSONObject();
            MultipartFile file = request.getFile("img");
            byte[] data = file.getBytes();
            OssResponse ossResponse = ossUpload(data, file.getOriginalFilename());
            if (ossResponse != null) {
                json.put("url",ossResponse.getOssImgUrl());
                json.put("success","true");
            } else {
                json.put("success", false);
            }
            String html = "<!DOCTYPE html>"+
                    "<html>"+
                    "<head>"+
                    "<script type="text/javascript">"+
    //                "document.domain = '127.0.0.1';"+
                    "</script>"+
                    "</head>"+
                    "<body>"+
                    json.toString()+
                    "</body>"+
                    "</html>";
            PrintWriter writer = response.getWriter();
            writer.print(html);
            writer.flush();
            writer.close();
        }

    后台代码输出结果:

    <!DOCTYPE html>

    <html>

    <head>

    <script type="text/javascript">

           document.domain = '127.0.0.1';

    </script>

    </head>

    <body>

    {"success":"true","url":"//****44f64df0f06b40369a8cc7531ce77533.jpg"}

    </body>

    </html>

    解决方法:去掉  "document.domain = '127.0.0.1';" 即可

     
  • 相关阅读:
    洛谷P1908《逆序对》
    洛谷P3884《[JLOI2009]二叉树问题》
    最近公共祖先 LCA
    洛谷P1531《I Hate It》
    洛谷P1563「NOIP2016」《玩具谜题》
    乘法逆元求法
    CF56E 【Domino Principle】
    CF638C 【Road Improvement】
    Luogu
    2018.8.7提高B组模拟考试
  • 原文地址:https://www.cnblogs.com/liuxiutianxia/p/8743987.html
Copyright © 2011-2022 走看看