简体   繁体   English

第二个Javascript表单POST在Chrome中失败,适用于IE和FF

[英]Second Javascript form POST fails in Chrome, works in IE and FF

I'm opening a new window via a javascript POST in response to a user click in a flash application. 我正在通过javascript POST打开一个新窗口,以响应用户在Flash应用程序中的点击。 The user may close the new window and want to re-open it by clicking the button on the first page again. 用户可以关闭新窗口,并希望再次单击第一页上的按钮重新打开它。 Since I have to pass some large arguments to the second page, I have to do a POST, a GET won't work. 因为我必须将一些大的参数传递给第二页,所以我必须进行POST,GET将无法工作。 So far I have the second page opening and behaving correctly in Chrome, FF, and IE the first time the button is clicked. 到目前为止,我第二次打开并在第一次点击按钮时在Chrome,FF和IE中正常运行。 However, in Chrome (it works in IE and FF) the second time the button is clicked, the POST is ignored and the new window is not opened. 但是,在Chrome(它在IE和FF中工作)第二次单击按钮时,将忽略POST并且不会打开新窗口。

Here's the function I use to do the POST. 这是我用来做POST的功能。 I have verified that it is making it all the way past the form.submit() line in Chrome with all the same parameters and no error notifications, but a new window still doesn't open. 我已经验证它是通过Chrome中的form.submit()行所有相同的参数并没有错误通知,但是新窗口仍然无法打开。

function post_to_url(path, paramString) {
    var params = paramString.split("|");

    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", path);
    form.setAttribute("target", "_blank");

    for (var i=0; i<params.length; i++) {
        var hiddenField = document.createElement("input");
        var param = params[i].split(":");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", param[0]);
        hiddenField.setAttribute("value", param[1]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form);
}

Any ideas on how this can be modified to get it to work in Chrome also? 有关如何修改它以使其在Chrome中运行的任何想法吗?

Update: It appears that the form isn't removed from the DOM in Chrome. 更新:似乎表单未从Chrome中的DOM中删除。 I'm not sure if that's the issue or not, but it should be removed regardless. 我不确定这是不是问题,但无论如何都应该删除。

We've also noticed that while 2nd POSTs in Chrome on Linux and Windows don't go through, they do on Macs. 我们也注意到,虽然Linux和Windows上Chrome中的第二个POST没有通过,但它们在Mac上运行。 However, even on a Mac the form isn't removed from the DOM. 但是,即使在Mac上,表单也不会从DOM中删除。

Another Update: Altering the code like this correctly removes the form from the DOM, but it doesn't solve the POST problem. 另一个更新:改变这样的代码正确地从DOM中删除表单,但它不能解决POST问题。

function post_to_url(path, paramString) {
    var postform = document.getElementById("postform");
    if (postform != null)
        document.body.removeChild(postform);
    var params = paramString.split("|");

    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", path);
    form.setAttribute("target", "_blank");
    form.id = "postform";

    for (var i=0; i<params.length; i++) {
        var hiddenField = document.createElement("input");
        var param = params[i].split(":");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", param[0]);
        hiddenField.setAttribute("value", param[1]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
    var postform = document.getElementById("postform"); 
    document.body.removeChild(postform);
}

I found a solution. 我找到了解决方案。 Adding a random number to the end of the URL causes Chrome to run the POST multiple times. 在URL末尾添加随机数会导致Chrome多次运行POST。 There must be a "feature" in Chrome that prevents multiple POSTs to the same URL. Chrome中必须有一个“功能”,可防止多个POST到同一个网址。

Here's what I changed: 这是我改变的:

form.setAttribute("action", path + '?' + Math.random().toString());

Make sure that a pop-up blocker isn't blocking the second open. 确保弹出窗口阻止程序没有阻止第二次打开。 I had a problem similar to this a little while ago. 不久前我遇到了类似的问题。

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

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