這是日常工作中整理出來的一份小抄。先記原則:GET 用於查詢與刪除,POST 用於新增與修改;特例是登入一定用 POST,因為帳號密碼不能暴露在網址列上。
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript">
function Go() {
window.location.href = "localhost:21811/Handler1.ashx?id=1&name='abc'";
}
</script>
</head>
<body>
<!-- 第一種:直接在網址後接參數 -->
localhost:21811/Handler1.ashx?id=1&name="abc"
<!-- 第二種:用超連結傳遞。點擊後跳轉並帶上 id 與 name -->
<a href="localhost:21811/Handler1.ashx?id=1&name='abc'">超鏈接傳遞參數</a>
<!-- 第三種:透過 JS 方法。點擊按鈕觸發 onclick 執行 Go() -->
<input type="button" onclick="Go()" value="通過js方法傳遞參數" />
<!-- 第四種:透過 form 表單傳遞 -->
<form action="Handler1.ashx" method="get"><!-- action 裡不要再帶參數 -->
<input type="text" name="id" value="3" />
<input type="text" name="name" value="abc" />
<input type="submit" value="通過表單傳遞參數" />
</form>
</body>
</html>
四種方式各有適用場景:需要使用者可書籤、可分享、可回退的情境,用超連結或直接拼網址最合適;需要依條件動態決定目標,用 JS 跳轉;涉及多個欄位、欄位可能變動,就交給 form。而 form 的 action 保持乾淨、參數全交給 method="get" 序列化,是最不容易出錯的習慣。