dojox/form/Uploader using iframe breaks on first upload after first server restart in Chrome

Copied from my original question on Stack Overflow at https://stackoverflow.com/questions/49237209/dojox-form-uploader-with-iframe-error-parsing-server-result-typeerror-cannot-r.

I have migrated a page with an iframe uploader from Dojo 1.8 to Dojo 1.9.3. The problem I have is that, ONLY in Chrome (both IE and Firefox work fine in this respect), the very first upload request to the server after Tomcat has started encounters the error shown above, specifically at the following section in “dojo/request/iframe.js”. I am using a textarea in the response, as required by the Dojo documentation.

function _36(_37, _38) {
    if (!_38) {
        try {
            var _39 = _37.options
              , doc = _10.doc(_10._frame)
              , _3a = _39.handleAs;
            if (_3a !== "html") {
                if (_3a === "xml") {
                    if (doc.documentElement.tagName.toLowerCase() === "html") {
                        _8("a", doc.documentElement).orphan();
                        var _3b = doc.documentElement.innerText;
                        _3b = _3b.replace(/>\s+</g, "><");
                        _37.text = _6.trim(_3b);
                    } else {
                        _37.data = doc;
                    }
                } else {
// the below line throws the TypeError.
                    _37.text = doc.getElementsByTagName("textarea")[0].value;
                }
                _5(_37);
            } else {
                _37.data = doc;
            }
        } catch (e) {
            _38 = e;
        }
    }
    if (_38) {
        this.reject(_38);
    } else {
        if (this._finished) {
            this.resolve(_37);
        } else {
            this.reject(new Error("Invalid dojo/request/iframe request state"));
        }
    }
}

This is the Uploader setup code I’m using. I should note that this code has been barely changed from the Dojo 1.8 version, which didn’t use the AMD module pattern for this particular component.

function setupFileUpload() {
    var upload = new dojox.form.Uploader({
        label: uploadButtonMessage,
        multiple:false,
        force: "iframe",
        uploadOnSelect:true, 
        url:"phaseBundleUpload.action",
        name: "upload",
        
        onChange: function(/*Array*/ fileArray) {
            var actionStatus = dom.byId("globalError");
            actionStatus.className = "globalInfo";
            actionStatus.innerHTML = 
                globalInfoMessage + 
                uploadInProgressMessage + 
                '<span>&nbsp;</span><img src="' + progressImageUrl + '" title="' + uploadInProgressMessage + '" border="0">';
                
            var uploadedFiles = dom.byId('uploadedFiles');
            // clear the uploaded filescombobox
            uploadedFiles.options.length = 0; // remove all options from the select
        },
    
        onError: function(/*Object or String*/ evtObject){
            var actionStatus = dom.byId("globalError");
            actionStatus.className = "globalError";
            actionStatus.innerHTML =  globalErrorMessage + uploadErrorMessage;
        },
        
        onComplete: function(arr) {
            var actionStatus = dom.byId("globalError");
            if(arr.errorMessage != null && arr.errorMessage != "") {
                actionStatus.className = "globalError";
                actionStatus.innerHTML =  globalErrorMessage + arr.errorMessage;
            } else {
                actionStatus.className = "globalInfo";
                actionStatus.innerHTML = globalInfoMessage + uploadSuccessMessage;
                buildUploadedFilesList(arr);
                setUploadedFilesPath(arr);
            }
            this.reset();
        },
        
        getFileList: function(){
            return {};
        }
    }, "upload");
    
  upload.startup();
}

I’ve done some debugging in Chrome, and from what I can tell, the response to the request is processed too soon, i.e. while the request is still pending. Because of that, the request doesn’t actually have a response yet, and doc isn’t yet properly defined. I have no idea why this happens like this, or why it only happens in Chrome or on the first upload request. The first request works fine in other browsers, and requests from the second and onwards also work fine. It’s just the very first request that fails.

What am I doing wrong here?

I would suggest that if you think this is a timing issue, to introduce a brief timeout or wrap things that might be async in a dojo/when call to see if that helps.