ajax, javascript的基本学习

来源:互联网 发布:中国人大网络教育 编辑:程序博客网 时间:2024/06/05 07:01

ajax区域获取数据操作是根据标签对象来识别的。

js中,每个有名字的标签都可以成为一个js中的对象。

下面是一个js页面的代码:index.jsp调用getData.jsp验证用户名是否可用!!

index.jsp

<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>HelloWorld Cheney Yao</title><meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page"><script type="text/javascript">function test(){    var url = "/ajax/getData.jsp?id="+document.getElementById("ddd").value;    sendRequest(url);}var XMLHttpReq = false;//创建XMLHttpRequest对象       function createXMLHttpRequest() {    if(window.XMLHttpRequest) { //Mozilla 浏览器        XMLHttpReq = new XMLHttpRequest();    }    else if (window.ActiveXObject) { // IE浏览器        try {            XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");        } catch (e) {            try {                XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");            } catch (e) {}        }    }}//发送请求函数function sendRequest(url) {createXMLHttpRequest();    XMLHttpReq.open("GET", url, false);    XMLHttpReq.onreadystatechange = processResponse;//指定响应函数    XMLHttpReq.send(null);  // 发送请求}// 处理返回信息函数function processResponse() {    if (XMLHttpReq.readyState == 4) { // 判断对象状态        if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息            var result = XMLHttpReq.responseText;             window.alert(result);               //system.out.println("result",result);            document.getElementById("data1").innerHTML = result;             } else { //页面不正常            window.alert("您所请求的页面有异常。");        }    }}</script></head><body><h1>my first heading </h1><p>my first paragraph</p><%//request the error contentrequest.setCharacterEncoding("gb2312");String error = request.getParameter("error");//error = new String(error.getBytes("ISO-8859-1"), "gb2312");//decode the request variif(error!=null){System.out.println("the vari in url:"+error);error = java.net.URLDecoder.decode(error, "gb2312");System.out.println("the vari after decode:"+error);}%><form action="/main/loginCh.jsp" method="post"><div>账号:<input type="text" name="id" id="ddd"size="20" value="yao" onblur="test()"/><div id="data1"></div></div><p>密码:<input type="password" name="psd" size="21" value="yao"/></p><input type="submit" value="Submit" /><input type="reset" value="RESET" /></form><a href="/main/register.jsp">click to register</a><%if(error!=null){%><%="error is:"+error %><%}%></body></html>

getData.jsp


<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><%   CheckLogin cl = new CheckLogin();if(cl.existCheck(request.getParameter("id"))){out.println("ok!");}else{out.println("exists!");}%></body></html>