發送請求參數

xiaoxiao2021-03-01  16

如果在請求時,要連帶發送相關參數,若是使用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!"並顯示在網頁上。
转载请注明原文地址: https://www.6miu.com/read-3850195.html

最新回复(0)