1

我一直在无情地搜索,但就是无法弄清楚这一点。为什么这个 XHR 连接在 Firefox中可以正常工作,但在 Chrome 中会中断?顺便说一句,我将它与 AngularJS 结合使用。

$scope.upload = function(project, file) {
                var formData = new FormData();  //Not really sure why we have to use FormData().  Oh yeah, browsers suck.
                formData.append('', file.file); //The real file object is stored in this file container

                file.xhr = new XMLHttpRequest();
                file.xhr.open('PUT', '/api/projects/'+project._id+'/files', true);

                //Progress event listener
                file.xhr.upload.onprogress = function(event) {
                    if(event.lengthComputable) {
                        file.uploadPercent = Math.round(event.loaded / event.total * 100);
                    }
                };

                //Upload complete listener
                file.xhr.upload.onload = function(event) {
                    file.uploaded = true;
                };

                //Every time the status changes
                file.xhr.onreadystatechange = function(event) {
                    if(event.target.readyState == 4) {
                        //The file has been added, so tag the ID onto the file object
                        console.log(event.target.responseText);
                        file._id = JSON.parse(event.target.responseText)._id;
                    } else {
                        return;
                    }
                };

                file.xhr.send(formData);
            };

在 Firefox 中,文件被很好地发送到我的服务器,并且responseText完全按照我的预期返回。但是,在 Chrome 中,我收到此错误: Error: INVALID_STATE_ERR: DOM Exception 11 Error: An attempt was made to use an object that is not, or is no longer, usable.如果它准确地告诉我尝试使用哪个对象,这将更有帮助。我在这里读到我应该尝试设置async为 false 和 use onreadystatechange,但我不确定这有什么帮助,因为我已经在使用onreadystatechange.

4

1 回答 1

0

2009 年的错误:提交表单时 XMLHttpRequest 不起作用 - https://bugs.webkit.org/show_bug.cgi?id=23933

于 2013-09-07T11:37:09.180 回答