【发布时间】:2014-03-09 15:50:32
【问题描述】:
我在看这个POST,我想知道,有没有办法让这个功能正常工作,但又不重定向到不同的页面?
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 调用即可发送表单数据。
-
你能再解释一下吗?
-
好的,我明白了,谢谢。最后一件事是它们之间有什么区别
-
了解 AJAX 的工作原理,一切都会变得清晰。基本上,它是一个异步调用(它发生在页面的“后面”),但您可以等待它的响应并根据该响应相应地修改您的页面。它还有更多内容,但这会让你开始。见这里:developer.mozilla.org/en/docs/AJAX
标签: javascript url post attributes