在OpenWindow中以Post取得網頁

  • 1495
  • 0

在OpenWindow中以Post取得網頁

不論是瀏覽器也好還是一般我們預設取得網頁頁面也好,大多都是採用Http動詞中的-Get。但是如果想要改以另一個常見的動詞-Post來取得網頁,其實也不太困難,如果是使用jQuery這個方便的Js套件大抵上就是一個指令的差別:

  • Get -  $.get(‘網頁Url’,參數,回呼函數)
  • Post- $.post(‘網頁Url’,參數,回呼函數)

 

但是最近在工作上有一個需求,該需求是要在Openwindow中以Post的方式去取得頁面,我找了一下發現還是有辦法使用Post取得頁面,Js程式碼大抵如下:

     var url = "http:/HostName:PortNo/ControllerName/ActionName";
     var newWindow = window.open(url, "Lobby");

      if (!newWindow) return false;

     var html = "";
     html += "<html><head></head><body><form id='formid' method='post' action='" + url + "'>";
     html += "<input type='hidden' name='" + ID+ "' value='" + ID+ "'/>";
     html += "<input type='hidden' name='" + Lang + "' value='" + Lang + "'/>";   
     html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</script></body></html>";

     newWindow.document.write(html);
     return newWindow;
}

在伺服器端的我是此用ASP.Net MVC,因此在上面的Js範例中,Url會寫上ControllerName與ActionName