其实到底要用哪个,哪个好,就要看你的实际需求。就我而言,ajax呢就主要用在局部刷新这一块,用的挺多的,其他就要看你平时工作中遇到的,然后总结来看了,不能断章取义的评判。
这只是我的个人观点,有什么问题我们可以互相交流,望采纳,谢谢
给你一个例子你参考参考<html>
<head>
<title>ajax调用servlet进行异步验证</title>
<script language="javascript">
var xmlHttp
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
//alert("create activeXObject")
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest()
//alert("create XMLHttpRequest")
}
}
function validate(){
//alert("run in validate()")
createXMLHttpRequest()
var date = document.getElementById("birthDate")
var url = "servlet/validationServlet?birthDate="+escape(date.value)
xmlHttp.open("GET",url,true)
xmlHttp.onreadystatechange = callback
xmlHttp.send(null)
}
function callback(){
//alert("run in callback()请求状态:"+xmlHttp.readyState)
if(xmlHttp.readyState == 4){
//alert("状态码:"+xmlHttp.states+"-"+xmlHttp.statesText)
alert(xmlHttp.responseText)
if(xmlHttp.status == 200){
var mes = xmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.data
var val = xmlHttp.responseXML.getElementsByTagName("passed")[0].firstChild.data
//alert( "begin to set message!")
setMessage(mes,val)
}
}
}
function setMessage(message,isValid){
var messageArea = document.getElementById("dateMessage")
var fontColor = "red"
if(isValid == "true"){
fontColor = "green"
}
messageArea.innerHTML = "<font color="+fontColor+">"+message+"</font>"
}
</script>
</head>
<body>
<h1>AJAX日期的自动验证</h1>
日期:<input type="text" size="10" id="birthDate" onKeyup="validate()"/>
<div id="dateMessage"></div>
</body>
</html>
------------------------------------------
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
boolean isIn
System.out.println("run in ValidationServlet")
PrintWriter out = response.getWriter()
boolean passed = validateDate(request.getParameter("birthDate"))
// try {
//// isIn = inSertIntoTable()
// System.out.println("插入成功")
// } catch (Exception e) {
// System.out.println("插入错误")
// }
response.setContentType("text/html")
// response.setContentType("text/xmlcharset=UTF-8")
response.setHeader("Cache-Control","no-cache")
String message = "You have entered an invalid date."
if(passed){
message = "invalid date is success."
}
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
out.println("<response>")
out.println("<passed>"+Boolean.toString(passed)+"</passed>")
out.println("<message>"+message+"</message>")
out.println("</response>")
out.close()
}
private boolean validateDate(String date) {
boolean isValid = true
if(date != null){
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy")
try{
format.parse(date)
}catch(Exception e){
isValid = false
}
}else{
isValid = false
}
return isValid
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)