诡异的跨域问题

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. (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 = ""+
                ""+
                ""+
                ""+
                ""+
                ""+
                json.toString()+
                ""+
                "";
        PrintWriter writer = response.getWriter();
        writer.print(html);
        writer.flush();
        writer.close();
    }

 

后台代码输出结果:

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

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

 

 

转载于:https://www.cnblogs.com/liuxiutianxia/p/8743987.html

你可能感兴趣的:(json,javascript,ViewUI)