iframe的contentDocument正文为null

时间:2018-11-20 15:04:15

标签: javascript html iframe

我正在使用以下与iframe相关的代码上传文件。

  function createAttachmentRequest(form, action_url, div_id){           
            document.getElementById("CreateAttachmentForm").elements["userId"].value = m_userID;
            document.getElementById("CreateAttachmentForm").elements["password"].value = m_password_e;
            document.getElementById("CreateAttachmentForm").elements["METHOD"].value = "POST";
            document.getElementById("CreateAttachmentForm").elements["parentIds"].value = strIssueID;

              // Create the iframe...
            var iframe = document.createElement("iframe");   
            iframe.setAttribute("id", "upload_iframe");
            iframe.setAttribute("name", "upload_iframe");
            iframe.setAttribute("width", "0");
            iframe.setAttribute("height", "0");
            iframe.setAttribute("border", "0");
            iframe.setAttribute("style", "width: 0; height: 0; border: none;");

            // Add to document...
            form.parentNode.appendChild(iframe);
            window.frames['upload_iframe'].name = "upload_iframe";

            iframeId = document.getElementById("upload_iframe");

            // Add event...
            var eventHandler = function () {

                if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
                else iframeId.removeEventListener("load", eventHandler, false);

                // Message from server...
                if (iframeId.contentDocument) {
                    content = iframeId.contentDocument.body.innerHTML;
                } else if (iframeId.contentWindow) {
                    content = iframeId.contentWindow.document.body.innerHTML;
                } else if (iframeId.document) {
                    content = iframeId.document.body.innerHTML;
                }               

                if( content.indexOf('HTTP Status') !== -1){
                    document.getElementById("IssueFileUploadHead").innerHTML = 'There is an error. Please contact Administrator';   
                }else{
                    //get the response string and return the object id from the response string
                    document.getElementById("IssueFileUploadHead").innerHTML = content;
                    var responseString = document.getElementById("IssueFileUploadHead").innerText;
                    document.getElementById("IssueFileUploadHead").innerHTML = "Uploading...";
                    var responseTag = "<response>";
                    var responseEnd = "</response>";
                    var objId = responseString.match(new RegExp(responseTag + "(.*)" + responseEnd));

                    if(objId && objId.length>1){
                        window.parent.ModalHelper.setRetVal(objId[1]);
                    }
                    window.parent.ModalHelper.close();
                }

                // Del the iframe...
                setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
            };

            if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
            if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);

            // Set properties of form...
            form.setAttribute("target", "upload_iframe");
            form.setAttribute("action", action_url);
            form.setAttribute("method", "post");
            form.setAttribute("enctype", "multipart/form-data");
            form.setAttribute("encoding", "multipart/form-data");

            // Submit the form...
            form.submit();
            document.getElementById("IssueFileUploadHead").innerHTML = "Uploading...";
        }   

从休息服务获得响应后,iframeId.contentDocument.body变为空。看来iframeId.contentDocument没有身体。关于这里出了什么问题以及如何使它起作用的任何线索? 现在,我将整个功能添加到了问题中。请现在看看。

0 个答案:

没有答案