2.2 发送请求 2.2.3 发送POST请求
发送步骤
发送POST请求通常需要执行如下三个步骤:
- 1.使用
open方法打开连接时,指定使用POST方式发送请求。 
- 2.设置正确的请求头,对于
POST请求通常应设置Content-Type请求头。 
- 3.发送请求,把请求参数转换为查询字符串(就是
key=value&key=value...这种形式),将该字符串作为send()方法的参数。 
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
   |  var xhr = new XMLHttpRequest();
  function change(id) {          var uri = "second.jsp";          xhr.open("POST", uri, true);          xhr.onload = processResponse;          xhr.setRequestHeader("Content-Type",             "application/x-www-form-urlencoded");          xhr.send("id=" + id); }
 
  | 
 
事实上,即使采用POST方式发送请求,一样可以将请求参数附加在请求的URL之后。将change函数改为如下形式也可
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   |  function change(id) {          var uri = "second.jsp?id="+id;          xhr.open("POST", uri, true);          xhr.onload = processResponse;          xhr.setRequestHeader("Content-Type",             "application/x-www-form-urlencoded");          xhr.send(); }
 
  | 
 
完整代码
项目结构
1 2 3 4 5 6 7 8
   | E:\workspace_web2\post └─WebContent   ├─first.html   ├─META-INF   │ └─MANIFEST.MF   ├─second.jsp   └─WEB-INF     └─lib
   | 
 
这是一个java web项目把这下两个文件放在Java web项目的根目录下即可运行.
first.html:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
   | <!DOCTYPE html> <html> <head> <meta name="author" content="Yeeku.H.Lee(CrazyIt.org)" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>发送GET请求</title> <style type="text/css"> select {     width: 160px;     font-size: 11pt; } </style> </head> <body>     <!-- 一级菜单 -->     <select name="first" id="first" size="4"         onchange="change(this.value);">         <option value="1" selected="selected">中国</option>         <option value="2">美国</option>         <option value="3">日本</option>     </select>     <!-- 二级菜单 -->     <select name="second" id="second" size="4">     </select>     <script type="text/javascript">                                          var xhr = new XMLHttpRequest();
 
 
 
 
 
 
 
 
 
 
 
 
 
                                           function change(id) {                                                  var uri = "second.jsp?id="+id;                                                  xhr.open("POST", uri, true);                                                  xhr.onload = processResponse;                                                  xhr.setRequestHeader("Content-Type",                                 "application/x-www-form-urlencoded");                                                  xhr.send();                     }                                          function processResponse() {                                                  if (xhr.status == 200) {                                                          var cityList = xhr.responseText.split("$");                                                          var displaySelect = document                                     .getElementById("second");                                                          displaySelect.innerHTML = "";                                                                                       for (var i = 0; i < cityList.length; i++) {                                                                  var op = document.createElement("option");                                 op.innerHTML = cityList[i];                                                                  displaySelect.appendChild(op);                             }                         } else {                                                          window.alert("您所请求的页面有异常。");                         }                     }                                          document.body.onload = change(1);                 </script> </body> </html>
   | 
 
second.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
   | <%@ page contentType="text/html; charset=utf-8" language="java" %> <% String idStr = (String)request.getParameter("id"); int id = idStr == null ? 1 : Integer.parseInt(idStr); System.out.println(id); switch(id) { 	case 1: %> 上海$广州$北京 <% 	break; 	case 2: %> 华盛顿$纽约$加州 <% 	break; 	case 3: %> 东京$大板$福冈 <% 	break; } %>
   | 
 
原文链接: 2.2 发送请求 2.2.3 发送POST请求