700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > js使用form表单方式模拟post请求

js使用form表单方式模拟post请求

时间:2021-05-05 23:25:05

相关推荐

js使用form表单方式模拟post请求

var openPostWindow =function (url, data1) {//url 为请求地址,data1为需要请求的数据var tempForm = document.createElement("form");//创建form表单,以下数form表单的各种参数tempForm.id = "tempForm1";tempForm.method = "post";tempForm.action = url;var hideInput1 = document.createElement("input");//创建标签 <input></input> 标签 然后设定属性,最后追加为 form标签的子标签hideInput1.type = "hidden";hideInput1.name = "result";hideInput1.value = data1;var hideInput2 = document.createElement("input");hideInput2.type = "hidden";hideInput2.name = "CSRFToken";hideInput2.value = JSON.parse(data1).CSRFToken;var hideInput3 = document.createElement("input");hideInput3.type = "hidden";hideInput3.name = "l";hideInput3.value = data1.length;tempForm.appendChild(hideInput1);tempForm.appendChild(hideInput2);tempForm.appendChild(hideInput3);if (document.all) {tempForm.attachEvent("onsubmit", function () {}); //IE} else {var subObj = tempForm.addEventListener("submit", function () {}, false); //firefox}document.body.appendChild(tempForm);if (document.all) {tempForm.fireEvent("onsubmit");} else {tempForm.dispatchEvent(new Event("submit"));}tempForm.submit();//提交POST请求document.body.removeChild(tempForm);//删除整个form标签}

亲测可用!!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。