使用虚拟表单提交post请求

使用模拟表单提交请求

function post(URL, PARAMS) {
  var temp = document.createElement("form");
  temp.action = URL;
  temp.method = "post";
  temp.style.display = "none";
  for (var x in PARAMS) {
    var opt = document.createElement("textarea");
    opt.name = x;
    opt.value = PARAMS[x];
    // alert(opt.name)
    temp.appendChild(opt);
  }
  document.body.appendChild(temp);
  temp.submit();
  return temp;
}

 调用方法:

post('pages/statisticsJsp/excel.action', {html :prnhtml,cm1:'sdsddsd',cm2:'haha'});

来自:https://www.jb51.net/article/75819.htm

 

另一种模拟提交的方式

参考:https://blog.csdn.net/HG13542386794/article/details/117013335

 

       function postData() {
            console.log($('#form_id1').serializeArray())
                var form = document.getElementById("form_id1");
                //1./Main/GetData 通过形参接收数据
                form.action = "/email_notice/hwplan/update_hwplan";
                form.method = "post";
                form.submit();
        }

注意使用form.submit()方法时,在form表单里不要有元素的id或者name被写作submit,否则会报Uncaught TypeError: form.submit is not a function

参考:https://blog.csdn.net/xcxy2015/article/details/77297040

posted @ 2023-03-30 13:25  声声慢43  阅读(74)  评论(0编辑  收藏  举报