<input>表单并提交
function post(path, params, method='post') { // The rest of this pre assumes you are not using a library. // It can be made less wordy if you use one. const form = document.createElement('form'); form.method = method; form.action = path; for (const key in params) { if (params.hasOwnProperty(key)) { const hiddenField = document.createElement('input'); hiddenField.type = 'hidden'; hiddenField.name = key; hiddenField.value = params[key]; form.appendChild(hiddenField); } } document.body.appendChild(form); form.submit();}
例:
post('/contact/', {name: 'Johnny Bravo'});
编辑 :既然已经被大肆抨击了,我猜人们会大量复制粘贴。因此,我添加了
hasOwnProperty检查以修复所有无意的错误。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)