简体   繁体   English

Google Chrome浏览器:执行POST时XMLHttpRequest.send()不起作用

[英]Google Chrome: XMLHttpRequest.send() not working while doing POST

I'm working on an application that allows the user to send a file using a form (a POST request), and that executes a series of GET requests while that file is being uploaded to gather information about the state of the upload. 我正在开发一个应用程序,该应用程序允许用户使用表单(POST请求)发送文件,并在上传文件时执行一系列GET请求,以收集有关上传状态的信息。

It works fine in IE and Firefox, but not so much in Chrome and Safari. 它在IE和Firefox中可以正常工作,但在Chrome和Safari中则不能。

The problem is that even though send() is called on the XMLHttpRequest object, nothing is being requested as can be seen in Fiddler. 问题在于,即使在XMLHttpRequest对象上调用了send(),也没有请求任何内容,就像在Fiddler中看到的那样。

To be more specific, an event handler is placed on the "submit" event of the form, that places a timeout function call on the window: 更具体地说,将事件处理程序放置在窗体的“ submit”事件上,该事件处理程序在窗口上放置一个超时函数调用:

window.setTimeout(startPolling, 10);

and in this function "startPolling" sequence is started that keeps firing GET requests to receive status updates from a web service that returns text/json that can be used to update the UI. 并在此函数中启动“ startPolling”序列,该序列不断触发GET请求,以从Web服务接收状态更新,该服务返回可用于更新UI的text / json。

Is this a limitation (perhaps security-wise?) on WebKit based browsers? 这是基于WebKit的浏览器的限制(也许是安全性吗?)? Is this a Chrome bug? 这是Chrome错误吗? (I'm seeing the same behaviour in Safari though). (虽然我在Safari中看到了相同的行为)。

I am having the exact same problem. 我现在正在遇到一模一样的问题。 At the moment i use an iframe, which is targeted in the form. 目前,我使用的是iframe,它以表格形式定位。 That allows the xhr requests to be executed while posting. 这样可以在发布时执行xhr请求。 While that does work, it doesn't degrade gracefully if someone disables javascript.(I couldn't load the next page outside the iframe without js) So if someone has a nicer solution, i would be grateful to hear it. 虽然这确实有效,但是如果有人禁用了javascript,它不会优雅地降级。(如果没有js,我无法在iframe之外加载下一页)因此,如果有人有更好的解决方案,我将不胜感激。

Here the jQuery script for reference: 这里的jQuery脚本可供参考:

$(function() {
$('form[enctype=multipart/form-data]').submit(function(){ 
    // Prevent multiple submits
    if ($.data(this, 'submitted')) return false;

    var freq = 500; // freqency of update in ms
    var progress_url = '{% url checker_progress %}'; // ajax view serving progress info 

    $("#progressbar").progressbar({
        value: 0
    });

    $("#progress").overlay({ 
        top: 272, 
        api: true,
        closeOnClick: false,
        closeOnEsc: false, 
        expose: { 
            color: '#333', 
            loadSpeed: 1000, 
            opacity: 0.9 
        }, 
    }).load();

    // Update progress bar
    function update_progress_info() {
        $.getJSON(progress_url, {}, function(data, status){ 
            if (data) {
                var progresse = parseInt(data.progress);
                $('#progressbar div').animate({width: progresse + '%' },{ queue:'false', duration:500, easing:"swing" }); 
            }
            window.setTimeout(update_progress_info, freq);
        });
    };
    window.setTimeout(update_progress_info, freq);

    $.data(this, 'submitted', true); // mark form as submitted.
});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM