我正在尝试将浏览器指向另一个页面。如果我想要GET请求,我可以说

document.location.href = 'http://example.com/q=a';

但是,除非我使用POST请求,否则我试图访问的资源将无法正确响应。如果这不是动态生成的,我可能会使用HTML

<form action="http://example.com/" method="POST">
    <input type="hidden" name="q" value="a">
</form>

然后我只需从DOM提交表单。

但实际上,我希望JavaScript代码能够让我说

post_to_url('http://example.com/', {'q':'a'});

最好的跨浏览器实现是什么?

我需要一个改变浏览器位置的解决方案,就像提交表单一样。这不应该是异步的,也不应该使用XML,因此Ajax不是答案。


当前回答

您可以进行AJAX调用(可能使用Prototype.js或JQuery等库)。AJAX可以处理GET和POST选项。

其他回答

一种解决方案是生成表单并提交

function post_to_url(url, params) {
    var form = document.createElement('form');
    form.action = url;
    form.method = 'POST';

    for (var i in params) {
        if (params.hasOwnProperty(i)) {
            var input = document.createElement('input');
            input.type = 'hidden';
            input.name = i;
            input.value = params[i];
            form.appendChild(input);
        }
    }

    form.submit();
}

所以我可以用一个简单的

javascript:post_to_url('http://is.gd/create.php', {'URL': location.href});

不可以。您不能像提交表单一样提交JavaScript发布请求。

您可以拥有HTML格式的表单,然后使用JavaScript提交它。(如本页多次所述)。

您可以自己创建HTML,不需要JavaScript来编写HTML。如果有人这样建议,那就太傻了。

<form id="ninja" action="http://example.com/" method="POST">
    <input id="donaldduck" type="hidden" name="q" value="a">
</form>

您的函数将按照您想要的方式配置表单。

function postToURL(a,b,c){
    document.getElementById("ninja").action     = a;
    document.getElementById("donaldduck").name  = b;
    document.getElementById("donaldduck").value = c;
    document.getElementById("ninja").submit();
}

然后,像这样使用它。

postToURL("http://example.com/","q","a");

但我会省略函数,只做。

document.getElementById('donaldduck').value = "a";
document.getElementById("ninja").submit();

最后,样式决定在ccs文件中。

#ninja { 
    display: none;
}

就我个人而言,我认为表格应该用名字来称呼,但现在这并不重要。

使用此答案中提供的createElement函数,这是必要的,因为IE在使用document.createElement正常创建的元素上使用name属性时会中断:

function postToURL(url, values) {
    values = values || {};

    var form = createElement("form", {action: url,
                                      method: "POST",
                                      style: "display: none"});
    for (var property in values) {
        if (values.hasOwnProperty(property)) {
            var value = values[property];
            if (value instanceof Array) {
                for (var i = 0, l = value.length; i < l; i++) {
                    form.appendChild(createElement("input", {type: "hidden",
                                                             name: property,
                                                             value: value[i]}));
                }
            }
            else {
                form.appendChild(createElement("input", {type: "hidden",
                                                         name: property,
                                                         value: value}));
            }
        }
    }
    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form);
}

Prototype库包含一个Hashtable对象和一个“.toQueryString()”方法,它允许您轻松地将JavaScript对象/结构转换为查询字符串样式的字符串。由于post要求请求的“主体”是查询字符串格式的字符串,这允许Ajax请求作为post正常工作。以下是使用原型的示例:

$req = new Ajax.Request("http://foo.com/bar.php",{
    method: 'post',
    parameters: $H({
        name: 'Diodeus',
        question: 'JavaScript posts a request like a form request',
        ...
    }).toQueryString();
};

这里有三个选项。

标准JavaScript回答:使用框架!大多数Ajax框架都会抽象出一种简单的XMLHTTPRequest POST方法。自己生成XMLHTTPRequest请求,将post传递到open方法而不是get。(有关在XMLHTTPRequest(Ajax)中使用POST方法的更多信息。)通过JavaScript,动态创建表单,添加动作,添加输入,然后提交。