我正在尝试将浏览器指向另一个页面。如果我想要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不是答案。


当前回答

好吧,希望我读过所有其他帖子,这样我就不会浪费时间从Rakesh Pai的回答中创建这些帖子了。这里有一个递归解决方案,可以处理数组和对象。不依赖jQuery。

添加了一个段来处理整个表单应该像数组一样提交的情况。(即,项目列表周围没有包装对象)

/**
 * Posts javascript data to a url using form.submit().  
 * Note: Handles json and arrays.
 * @param {string} path - url where the data should be sent.
 * @param {string} data - data as javascript object (JSON).
 * @param {object} options -- optional attributes
 *  { 
 *    {string} method: get/post/put/etc,
 *    {string} arrayName: name to post arraylike data.  Only necessary when root data object is an array.
 *  }
 * @example postToUrl('/UpdateUser', {Order {Id: 1, FirstName: 'Sally'}});
 */
function postToUrl(path, data, options) {
    if (options === undefined) {
        options = {};
    }

    var method = options.method || "post"; // Set method to post by default if not specified.

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

    function constructElements(item, parentString) {
        for (var key in item) {
            if (item.hasOwnProperty(key) && item[key] != null) {
                if (Object.prototype.toString.call(item[key]) === '[object Array]') {
                    for (var i = 0; i < item[key].length; i++) {
                        constructElements(item[key][i], parentString + key + "[" + i + "].");
                    }
                } else if (Object.prototype.toString.call(item[key]) === '[object Object]') {
                    constructElements(item[key], parentString + key + ".");
                } else {
                    var hiddenField = document.createElement("input");
                    hiddenField.setAttribute("type", "hidden");
                    hiddenField.setAttribute("name", parentString + key);
                    hiddenField.setAttribute("value", item[key]);
                    form.appendChild(hiddenField);
                }
            }
        }
    }

    //if the parent 'data' object is an array we need to treat it a little differently
    if (Object.prototype.toString.call(data) === '[object Array]') {
        if (options.arrayName === undefined) console.warn("Posting array-type to url will doubtfully work without an arrayName defined in options.");
        //loop through each array item at the parent level
        for (var i = 0; i < data.length; i++) {
            constructElements(data[i], (options.arrayName || "") + "[" + i + "].");
        }
    } else {
        //otherwise treat it normally
        constructElements(data, "");
    }

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

其他回答

我使用document.forms并循环它以获取表单中的所有元素,然后通过XMLHttpRequest发送。这是我的javascript/ajax提交解决方案(以所有HTML为例):

函数smc(){var http=new XMLHttpRequest();var url=“yourphpfile.php”;var x=document.forms[0];var xstr=“”;变量i;对于(i=0;i<x.length;i++){如果(i==0){xstr+=x.elements[i].name+“=”+x.elements[i].value;}其他{xstr+=“&”+x.elements[i].name+“=”+x.element[i].value;}}http.open(“POST”,url,true);http.setRequestHeader(“内容类型”,“application/x-www-form-urlencoded”);http.onreadystatechange=函数(){如果(http.readyState==4&&http.status==200){//在这里使用html输出响应执行任何操作}}http.send(xstr);}<!DOCTYPE html><html><body><表单>名字:<input-type=“text”name=“fname”value=“Donald”><br>姓氏:<input-type=“text”name=“lname”value=“Duck”><br>地址1:<input-type=“text”name=“add”value=”123 Pond Dr“><br>城市:<input type=“text“name=”City“value=”Duckopolis“><br></form><button onclick=“smc()”>提交</button></body></html>

您可以使用类似jQuery的库及其$.post方法。

这在我的情况下非常有效:

document.getElementById("form1").submit();

您可以在以下函数中使用它:

function formSubmit() {
     document.getElementById("frmUserList").submit();
} 

使用此选项,您可以发布所有输入值。

我用来发布和引导用户自动到另一个页面的方法是只写一个隐藏表单,然后自动提交。请确保隐藏表单在网页上绝对不会占用空间。代码应该是这样的:

    <form name="form1" method="post" action="somepage.php">
    <input name="fielda" type="text" id="fielda" type="hidden">

    <textarea name="fieldb" id="fieldb" cols="" rows="" style="display:none"></textarea>
</form>
    document.getElementById('fielda').value="some text for field a";
    document.getElementById('fieldb').innerHTML="some text for multiline fieldb";
    form1.submit();

自动提交申请

自动提交的应用程序会将用户自动放入另一页的表单值引导回该页。此类应用程序如下所示:

fieldapost=<?php echo $_post['fielda'];>
if (fieldapost !="") {
document.write("<form name='form1' method='post' action='previouspage.php'>
  <input name='fielda' type='text' id='fielda' type='hidden'>
</form>");
document.getElementById('fielda').value=fieldapost;
form1.submit();
}

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