IE10 ajaxupload iframe.contentDocument对象无法访问

在IE10中使用ajaxupload.js(不跨域),一直报iframe.contentDocument无法访问。

ajaxupload.js中,_createForm如下:注意settings.data.ajax_token = 1; 为ajax请求,如果出错响应json。否则是html请求,如果出错响应html(error.jsp)

 _createForm: function (iframe) {
            var settings = this._settings;

            // We can't use the following code in IE6
            // var form = document.createElement('form');
            // form.setAttribute('method', 'post');
            // form.setAttribute('enctype', 'multipart/form-data');
            // Because in this case file won't be attached to request                    
            var form = toElement('<form method="post" enctype="multipart/form-data"></form>');

            form.setAttribute('action', settings.action);
            form.setAttribute('target', iframe.name);
            form.style.display = 'none';
            document.body.appendChild(form);

            settings.data.ajax_token = 1;

            // Create hidden input element for each data key
            for (var prop in settings.data) {
                if (settings.data.hasOwnProperty(prop)) {
                    var el = document.createElement("input");
                    el.setAttribute('type', 'hidden');
                    el.setAttribute('name', prop);
                    el.setAttribute('value', settings.data[prop]);
                    form.appendChild(el);
                }
            }
            return form;
        },

ajaxupload.js中,获取Response相应的代码:

 _getResponse: function (iframe, file) {
            // getting response
            var toDeleteFlag = false,
                self = this,
                settings = this._settings;

            addEvent(iframe, 'load', function () {

                if ( // For Safari 
                iframe.src == "javascript:'%3Chtml%3E%3C/html%3E';" ||
                // For FF, IE
                iframe.src == "javascript:'<html></html>';") {
                    // First time around, do not delete.
                    // We reload to blank page, so that reloading main page
                    // does not re-submit the post.

                    if (toDeleteFlag) {
                        // Fix busy state in FF3
                        setTimeout(function () {
                            removeNode(iframe);
                        },
                        0);
                    }

                    return;
                }

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

                // fixing Opera 9.26,10.00
                if (doc.readyState && doc.readyState != 'complete') .........................

原来是java端设置的HttpServletResponse问题:
原来的java代码:

private void processAjaxError(HttpServletRequest request, HttpServletResponse response, Exception e) {
        OperInfo operInfo = new OperInfo();
        operInfo.setIsOk(false);
        if (e instanceof BusiException|| e instanceof NoRollBackException) {
            response.setStatus(500);
            operInfo.setInfo(e.getMessage());
        } else {
            response.setStatus(500);
            operInfo.setInfo("系统异常,请联系系统管理员。");
            operInfo.setData("detail", ExceptionUtils.getFullStackTrace(e));
            logger.error("系统异常", e);
        }
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        try {
            response.getWriter().println(JSONObject.fromObject(operInfo).toString());
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

重新设置:
**// response.setStatus(400);
response.setContentType(“text/html”);**

改后代码:

private void processAjaxError(HttpServletRequest request, HttpServletResponse response, Exception e) {

        OperInfo operInfo = new OperInfo();
        operInfo.setIsOk(false);
        if (e instanceof BusiException|| e instanceof NoRollBackException) {
//          response.setStatus(400);
            operInfo.setInfo(e.getMessage());
        } else {
//          response.setStatus(500);
            operInfo.setInfo("系统异常,请联系系统管理员。");
            operInfo.setData("detail", ExceptionUtils.getFullStackTrace(e));
            logger.error("系统异常", e);
        }
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        try {
            response.getWriter().println(JSONObject.fromObject(operInfo).toString());
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

主要是http协议问题,还不是很懂,先记住了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值