js使用post 方式開啟新視窗

來源:互聯網
上載者:User

js使用post 方式開啟新視窗

  這篇文章主要介紹了js使用post 方式開啟新視窗的相關資料,需要的朋友可以參考下

  js中一般新開啟視窗很簡單直接window.open(url);就可以了,

  但是由於我希望傳遞參數到服務端,而且參數看起來很長一串,而且get方式的提交參數長度是有限制的,因此我有以下需求:

  1,js中實現post提交

  2,返回的頁面在新視窗顯示

  首先我是這麼做的:

  代碼如下:

  $.ajax({

  type: "POST",

  url: '${contextPath}/analyse/detail.do',

  data: {carNum :carNum,ids:refIds},

  success: function(str_response) { var obj = window.open("about:blank");

  obj.document.write(str_response);

  }

  });

  通過jQuery ajax提交,返回的資料寫在新的頁面中,但是由於瀏覽器的會攔截自動彈出的視窗,這樣還需使用者自己解除攔截,使用者體驗很差,

  然後我又通過類比form表單的提交來實現

  代碼如下:

  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);

  temp_form .submit();

  }

  注意:如需新開啟視窗 form 的target屬性要設定為'_blank'

 

  然後請求post('${contextPath}/analyse/detail.do',{carNum :carNum,ids:refIds});就可以了

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.