GET,通过url发送数据
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> <script language="JavaScript">
var xmlHttp; function login() { if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); }else{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } var name = document.getElementById("name").value; var password = document.getElementById("password").value; var url = "/Crawler/Login?name="+name+"&password="+password; xmlHttp.open("GET", url, true); xmlHttp.send(null); } </script> </head> <body> 用户名:<input type="text" name="name" id="name"/></br> 密码:<input type="password" name="password" id="password"/></br> <button type="button" onclick="login();">登陆</button> </body> </html>
POST:通过XMLHttpRequest.send(),发送xml格式数据,需在header中设置Content-Type:text/xml
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> <script language="JavaScript">
var xmlHttp;
function login() { if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); }else{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } alert("send ok") var name = document.getElementById("name").value; var password = document.getElementById("password").value; var xmlString = "<profile>" + " <name>" + escape(name) + "</name>" + " <password>" + escape(password) + "</password>" + "</profile>"; var url = "/Crawler/Login"; xmlHttp.open("POST", url, true); xmlHttp.setRequestHeader("Content-Type", "text/xml"); xmlHttp.send(xmlString);//发送数据 } </script> </head> <body> 用户名:<input type="text" name="name" id="name"/></br> 密码:<input type="password" name="password" id="password"/></br> <button type="button" onclick="login();">登陆</button> </body> </html>