最近剛好有這樣的需求...小弟測試心得分享給大家呀...
一般利用js的window.open開另一網(wǎng)要傳參數(shù)都用Get..方式..在url後面接參數(shù)...
但如果傳帳號.密碼..可能會被看到...不安全..去找了一下window.open在post上的用法...
JScript.js
- function openWindowWithPost(url, name, keys, values) {
- var newWindow = window.open(url, name);
- if (!newWindow) return false;
- var html = "";
- html += "<html><head></head><body><form id='formid' method='post' action='" + url + "'>";
- if (keys && values && (keys.length == values.length))
- for (var i = 0; i < keys.length; i++)
- html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";
- html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</script></body></html>";
- newWindow.document.write(html);
- return newWindow;
- }
OpenPost.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="OpenPost.aspx.cs" Inherits="OpenPost" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www./1999/xhtml">
- <head runat="server">
-
- <script src="JScript.js" type="text/javascript"></script>
-
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <input id="id" type="text" />
-
- <input id="pwd" type="password" />
-
- <input id="Button1" type="button" value="Post" onclick="Post();" />
- <input id="Button2" type="button" value="Get" onclick="Get();" />
- </div>
- </form>
- </body>
- </html>
-
- <script type="text/javascript">
- function Post() {
- var keys = [];
- var values = [];
- keys[0] = "id";
- keys[1] = "pwd";
- values[0] = document.getElementById("id").value;
- values[1] = document.getElementById("pwd").value;
- openWindowWithPost("result.aspx", "web", keys, values);
- }
- function Get() {
- window.open("result.aspx?id=" + document.getElementById("id").value + "&pwd=" + document.getElementById("pwd").value, "web");
- }
- </script>
OpenPost.aspx.cs(省略)
result.aspx(省略)
result.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class result : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Request.Form["id"] != null)
- {
- Response.Write("id:" + Request.Form["id"].ToString() + "<" + "br/>");
- }
-
- if (Request.Form["pwd"] != null)
- {
- Response.Write("pwd:" + Request.Form["pwd"].ToString() + "<" + "br/>");
- }
-
- if (Request.QueryString["id"] != null)
- {
- Response.Write("id:" + Request.QueryString["id"].ToString() + "<" + "br/>");
- }
-
- if (Request.QueryString["pwd"] != null)
- {
- Response.Write("pwd:" + Request.QueryString["pwd"].ToString() + "<" + "br/>");
- }
- }
- }
執(zhí)行結果:
POST結果:
GET結果:
參考網(wǎng)址:
http://www./forum/archive/index.php/t-1688.html