Javascript window.open使用POST传递值

php*_*der 65 javascript post get window

我有一个javascript函数,它使用window.open调用另一个页面并返回结果.

这是我的代码部分:

var windowFeatures = "status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=1, scrollbars=1";
window.open ('http://www.domain.com/index.php?p=view.map&coords=' + encodeURIComponent(coords), 'JobWindow', windowFeatures);
Run Code Online (Sandbox Code Playgroud)

我现在的问题是我要传递大量数据供GET处理,我需要使用POST方法传递它.

如何转换上面的代码以使用POST方法打开页面而不在整个页面上显示实现表单(该页面列出了包含供应商列表的100个订单 - 我正在尝试映射供应商)

php*_*der 66

我使用了上面的变体,但我没有打印html而是构建了一个表单并将其提交给第三方网址:

    var mapForm = document.createElement("form");
    mapForm.target = "Map";
    mapForm.method = "POST"; // or "post" if appropriate
    mapForm.action = "http://www.url.com/map.php";

    var mapInput = document.createElement("input");
    mapInput.type = "text";
    mapInput.name = "addrs";
    mapInput.value = data;
    mapForm.appendChild(mapInput);

    document.body.appendChild(mapForm);

    map = window.open("", "Map", "status=0,title=0,height=600,width=800,scrollbars=1");

if (map) {
    mapForm.submit();
} else {
    alert('You must allow popups for this map to work.');
}
Run Code Online (Sandbox Code Playgroud)

  • 删除该行的@Fedor将导致firefox上出现空白弹出窗口 (2认同)

Fac*_*tor 41

谢谢php-b-grader.我改进了代码,没有必要使用window.open(),目标已在表单中指定.

// Create a form
var mapForm = document.createElement("form");
mapForm.target = "_blank";    
mapForm.method = "POST";
mapForm.action = "abmCatalogs.ftl";

// Create an input
var mapInput = document.createElement("input");
mapInput.type = "text";
mapInput.name = "variable";
mapInput.value = "lalalalala";

// Add the input to the form
mapForm.appendChild(mapInput);

// Add the form to dom
document.body.appendChild(mapForm);

// Just submit
mapForm.submit();
Run Code Online (Sandbox Code Playgroud)

目标选项 - > w3schools - 目标


ref*_*med 20

对于它的价值,这里是以前提供的代码封装在一个函数中.

openWindowWithPost("http://www.example.com/index.php", {
    p: "view.map",
    coords: encodeURIComponent(coords)
});
Run Code Online (Sandbox Code Playgroud)

功能定义:

function openWindowWithPost(url, data) {
    var form = document.createElement("form");
    form.target = "_blank";
    form.method = "POST";
    form.action = url;
    form.style.display = "none";

    for (var key in data) {
        var input = document.createElement("input");
        input.type = "hidden";
        input.name = key;
        input.value = data[key];
        form.appendChild(input);
    }

    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form);
}
Run Code Online (Sandbox Code Playgroud)

  • 恕我直言,这是最好的答案。请注意,它会在发送后删除表单,清理 DOM。它也被编写为一个随时可用的函数。干得好 (2认同)

小智 7

谢谢php-b-grader!

在window.open的泛型函数下面使用POST传递值:

function windowOpenInPost(actionUrl,windowName, windowFeatures, keyParams, valueParams) 
{
    var mapForm = document.createElement("form");
    var milliseconds = new Date().getTime();
    windowName = windowName+milliseconds;
    mapForm.target = windowName;
    mapForm.method = "POST";
    mapForm.action = actionUrl;
    if (keyParams && valueParams && (keyParams.length == valueParams.length)){
        for (var i = 0; i < keyParams.length; i++){
        var mapInput = document.createElement("input");
            mapInput.type = "hidden";
            mapInput.name = keyParams[i];
            mapInput.value = valueParams[i];
            mapForm.appendChild(mapInput);

        }
        document.body.appendChild(mapForm);
    }


    map = window.open('', windowName, windowFeatures);
if (map) {
    mapForm.submit();
} else {
    alert('You must allow popups for this map to work.');
}}
Run Code Online (Sandbox Code Playgroud)