發送請求參數

如果在請求時,要連帶發送相關參數,若是使用GET的方式發送參數,則將參數附加在URL上即可,例如:
var urlAndqueryString = "yourApp?name=justin&age=30";
xmlHttp.open("GET", urlAndqueryString);
xmlHttp.send(null);

如果發送請求時使用POST,那麼將要發送的資料塞到send()中即可,例如:
var url = "yourApp";
var queryString = "name=justin&age=30";
xmlHttp.open(“POST", url);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(queryString);

由於塞到POST本體中的資料有可能是表單的name/value,也有可能是XML、 JSON 等格式,您必須告訴伺服端如何剖析表單本體內容,這可以設定 Content-Type的header來告知,以name/value或JSON格式來說,就要設定 Content-Typeapplication/x-www-form-urlencoded,如果是XML文件的話,則要設定 text/xml

以下以簡單的實例示範如何以GET及POST發送請求參數,假設您用以下的Servlet來處理請求:
  • GetPostServlet.java
package onlyfun.caterpillar;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetPostServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
public GetPostServlet() {
super();
}

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doResponse(request, response, "GET");
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doResponse(request, response, "POST");
}

private void doResponse(HttpServletRequest request,
HttpServletResponse response, String method)
throws ServletException, IOException {
String name = request.getParameter("name");

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println(method + ": Hello!" + name + "!");

out.flush();
out.close();
}
}

回應只是簡單的傳回發送的請求參數值,並加上GET或POST表示接收到何種請求,假設您使用以下的網頁來發送請求:
  • GETPOSTEx-1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>GET、POST</title>
<script type="text/javascript" src="GETPOSTEx-1.js"></script>
</head>
<body>
<input id="namefield" type="text" name="name"/>
<input value="GET" type="button" οnclick="doGetRequest();"/>
<input value="POST" type="button" οnclick="doPostRequest();"/>
<br>
<div id="response"></div>
</body>
</html>

網頁上分別有GET與POST兩個按鈕,按下後分別由GETPOSTEx-1.js中的doGetRequest()或doPostRequest()來發送GET、POST請求,假設GETPOSTEx-1.js撰寫如下:
  • GETPOSTEx-1.js
var xmlHttp;

function createXMLHttpRequest() {
if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}

function createQueryString() { // 建立請求參數
return "name=" + document.getElementById("namefield").value; // 取得文字方塊值
}

function doGetRequest() {
var url = "GetPostServlet?" + createQueryString();
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", url);
xmlHttp.send(null);
}

function doPostRequest() {
var url = "GetPostServlet?timeStamp=" + new Date().getTime(); // 避免快取
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("POST", url);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(createQueryString());
}

function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
document.getElementById("response").innerHTML =
xmlHttp.responseText;
}
}
}

由於POST時,網址URL並不會有所變化,在某些瀏覽器,如果請求的URL是相同的,則會利用快取中的資料,為了避免資料快取,則您可以故意加上一個timeStamp請求參數,附上當時系統的時間,如此每次請求時URL就不會相同。

完成以上程式後,在網頁文字方塊中輸入"Justin"並按下GET按鈕,則會傳回"GET: Hello!Justin!"並顯示在網頁上,按下POST按鈕,則會傳回"POST: Hello!Justin!"並顯示在網頁上。 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值