Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript在post上没有重定向_Javascript_Url_Post_Attributes - Fatal编程技术网

JavaScript在post上没有重定向

JavaScript在post上没有重定向,javascript,url,post,attributes,Javascript,Url,Post,Attributes,我正在看这个,我想知道,有没有一种方法可以让这个函数正常工作,但不重定向到其他页面 function post_to_url(path, params, method) { method = method || "post"; // Set method to post by default if not specified. // The rest of this code assumes you are not using a library. // It can

我正在看这个,我想知道,有没有一种方法可以让这个函数正常工作,但不重定向到其他页面

function post_to_url(path, params, method) {
    method = method || "post"; // Set method to post by default if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
         }
    }

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

我的问题在这一行
form.setAttribute(“action”,path)哪个发送到了不同的站点

您可以使用ajax发送请求。使用jQuery,它可以是这样的

$.post( "test.php", $( "#testform" ).serialize() );

我想iframes可以帮你。试用:)

在评论中修复后,以下是答案版本

您需要进行一个AJAX调用来发送表单数据,而不是使用表单。
了解AJAX是如何工作的,一切都会变得清晰。基本上,它是一个异步调用(发生在页面的“后面”),但好的一面是,您可以等待它的响应,并根据该响应相应地修改页面(例如,显示“谢谢”消息或错误等)

还有很多,但这会让你开始。请看这里:


此外,在线上有数百万个示例和教程,所以现在您知道需要做什么了,这样做就不会有任何问题。

只需打一个AJAX电话发送表单数据。您能再解释一下吗?好的,我知道了,谢谢。最后一件事是它们之间有任何区别了解AJAX的工作原理,一切都会变得清晰。基本上,它是一个异步调用(发生在页面的“后面”),但您可以等待它的响应,并根据该响应相应地修改页面。还有很多,但这会让你开始。看这里:你的答案有点短。此外,由于链接可能在将来某个时候中断,因此答案中不鼓励链接到外部站点。请用相关代码扩展您的答案。