诡异的跨域问题

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';" 即可

 

 

posted @ 2018-04-08 11:33  创可贴Y  阅读(505)  评论(0编辑  收藏  举报