打开一个新页面 并向新页面发送一个POST请求 新页面并不是自己的页面,而是别人的

当前有一个场景,需要PHP处理完逻辑后,调转到一个别人的新页面,并在此新页面发送一个POST请求。于是乎到处百度  

网上找来的资料,做下整理 

//模拟表单post提交,且打开新页面跳转

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

  //  使用方法url表示请求地址,后面传参类似ajax
post('url',{id:id,name:name,age:age});

以上代码  完美解决此问题。

posted @ 2022-01-25 18:05  树下水月  阅读(232)  评论(0编辑  收藏  举报