如果在求r,要Bl送相P担羰鞘褂GET的方式l送担t蹈郊釉URL上即可,例如:
var urlAndqueryString = “yourApp?name=justin&age=30”;
xmlHttp.open(“GET”, urlAndqueryString);
xmlHttp.send(null);
xmlHttp.open(“GET”, urlAndqueryString);
xmlHttp.send(null);
如果l送求r使用POST,那N⒁l送的Y料塞到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);
var queryString = “name=justin&age=30”;
xmlHttp.open(“POST”, url);
xmlHttp.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);
xmlHttp.send(queryString);
由於塞到POST本w中的Y料有可能是表蔚name/value,也有可能是XML、jsON 等格式,您必告V伺服端如何剖析表伪倔w热荩@可以O定Content-Type的header砀嬷name/value或JSON格式碚f,就要O定Content-Typeapplication/x-www-form-urlencoded,如果是XML文件的,t要O定text/xml。
以下以蔚例示如何以GET及POSTl送求担僭O您用以下的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();
}
}
回皇呛蔚骰匕l送的求抵担K加上GET或POST表示接收到何N求,假O您使用以下的W戆l送求:
- 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" />
<input value="POST" type="button" />
<br>
<div id="response"></div>
</body>
</html>
W上分e有GEtcpOST按o,按下後分e由GETPOSTEx-1.js中的doGetRequest()或doPostRequest()戆l送GET、POST求,假OGETPOSTEx-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; // 取得文字方K值
}
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;
}
}
}
由於POSTr,W址URLK不兴化,在某些g[器,如果求的URL是相同的,t每烊≈械馁Y料,榱吮苊赓Y料快取,t您可以故意加上一timeStamp求担缴袭r系y的rg,如此每次求rURL就不嗤
完成以上程式後,在W文字方K中入”Justin”K按下GET按o,t骰”GET: Hello!Justin!”K@示在W上,按下POST按o,t骰”POST: Hello!Justin!”K@示在W上。
欢迎大家阅读《_jquery》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码