除了 Fabrizio 的回答,有人编写了一个 javascript 函数,它允许您构建表单并POST
在运行时通过它发送。
POST
就像GET
(变量附加到 url)除了变量是通过标头发送的。仍然可以伪造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();
}
像这样使用:
post_to_url("http://mydomain.com/", {'page_id':'10'}, "post");
来源: JavaScript 发布请求,如表单提交