第一種:傳統(tǒng)的ajax異步請(qǐng)求,后臺(tái)代碼以及效果在最下邊
首先我們?cè)趀clipse中創(chuàng)建一個(gè)注冊(cè)頁(yè)面regist.jsp,創(chuàng)建一個(gè)form表單,注意,由于我們只是實(shí)現(xiàn)用戶(hù)名校驗(yàn)的效果,下邊紅色部門(mén)是我們需要研究對(duì)象,所以其他的部門(mén)可以忽略不看。
內(nèi)容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用戶(hù)注冊(cè)</title> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/css/login.css" rel="external nofollow" > <script type="text/javascript"> //第三步:ajax異步請(qǐng)求用戶(hù)名是否存在 function checkUsername(){ // 獲得文本框值: var username = document.getElementById("username").value; // 1.創(chuàng)建異步交互對(duì)象 var xhr = createXmlHttp();//第二步中已經(jīng)創(chuàng)建xmlHttpRequest,這里直接調(diào)用函數(shù)就可以了。 // 2.設(shè)置監(jiān)聽(tīng) xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if(xhr.status == 200){ //把返回的數(shù)據(jù)放入到span中 document.getElementById("span").innerHTML = xhr.responseText;//responseText是后臺(tái)返回的數(shù)據(jù) } } } // 3.打開(kāi)連接 xhr.open("GET","${pageContext.request.contextPath}/user_findByName.action?time="+new Date().getTime()+"&username="+username,true); // 4.發(fā)送 xhr.send(null); } //第二部:創(chuàng)建xmlHttp對(duì)象 function createXmlHttp(){ var xmlHttpRequest; try{ // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e){ try{// Internet Explorer xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){} } } return xmlHttpRequest; } function change(){ var img1 = document.getElementById("checkImg"); img1.src="${pageContext.request.contextPath}/checkImg.action?"+new Date().getTime(); } </script> </head> <body> <form action="${pageContext.request.contextPath }/user_regist.action" method="post" onsubmit="return checkForm()";> <div class="regist"> <div class="regist_center"> <div class="regist_top"> <div class="left fl">會(huì)員注冊(cè)</div> <div class="right fr"><a href="${pageContext.request.contextPath }/index.jsp" rel="external nofollow" target="_self">小米商城</a></div> <div class="clear"></div> <div class="xian center"></div> </div> <div class="regist_main center"> //第一步:首先,我們創(chuàng)建一個(gè)用戶(hù)名input輸入框,并添加一個(gè)onblur="checkUsername()"事件 <div class="username">用 戶(hù) 名: <input class="shurukuang" type="text" id="username" name="username" onblur="checkUsername()"/><span id="span"></span></div> <div class="username">密 碼: <input class="shurukuang" type="password" id="password" name="password"/></div> <div class="username">確認(rèn) 密碼: <input class="shurukuang" type="password" id="repassword" name="repassword" /></div> <div class="username">郵 箱 號(hào): <input class="shurukuang" type="email" id="email" name="email" /></div> <div class="username">姓 名: <input class="shurukuang" type="text" id="name" name="name"/></div> <div class="username">手 機(jī) 號(hào): <input class="shurukuang" type="text" id="phone" name="phone"/></div> <div class="username">地 址: <input class="shurukuang" type="text" id="addr" name="addr"/></div> <div class="username"> <div class="left fl">驗(yàn) 證 碼: <input class="yanzhengma" type="text" id="checkcode" name="checkcode" maxlength="4"/></div> <div class="right fl"><img id="checkImg" class="captchaImage" src="${pageContext.request.contextPath}/checkImg.action" onclick="change()" title="點(diǎn)擊更換驗(yàn)證碼"></div> <div class="clear"></div> </div> </div> <div class="regist_submit"> <input class="submit" type="submit" name="submit" value="立即注冊(cè)" > </div> </div> </div> </form> </body> </html>
第二種方式:使用jQuery中的ajax實(shí)現(xiàn)以上效果。首先f(wàn)orm表單以及Action中的都不變,我們只需改變script就可以了。
第一步:引入js文件<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-3.2.1.min.js"></script>
第二步:
//ajax異步請(qǐng)求用戶(hù)名是否存在 $(function(){ $('#username').change(function(){//給username添加一個(gè)change事件 var val = $(this).val();//獲取輸入框的值 val = $.trim(val);//去空 if(val != ""){//判斷值是否為空 var url = "${pageContext.request.contextPath}/user_findByName.action";//url還是那個(gè)URL var args ={"time":new Date().getTime(),"username":val};//這里和上面不同的是,這里用json方式實(shí)現(xiàn)傳入的time和username參數(shù) $.post(url,args,function(data){//發(fā)送post請(qǐng)求,后臺(tái)返回的數(shù)據(jù)在data里面, $('#span').html(data);//把后臺(tái)返回的數(shù)據(jù)放入span中 }); } }); })
然后我們來(lái)看一下后臺(tái)數(shù)據(jù)上會(huì)怎么返回的。由于我這是使用ssh框架實(shí)現(xiàn)的,為了方便,所以我只展示在Action中是怎么返回?cái)?shù)據(jù)的,關(guān)于ssh框架中service層,dao層的實(shí)現(xiàn)請(qǐng)自行解決。
public class UserAction extends ActionSupport implements ModelDriven<User> { private static final long serialVersionUID = 1L; /** * 模型驅(qū)動(dòng) */ private User user = new User(); @Override public User getModel() { return user; } // 注入U(xiǎn)serService private UserService userService; public void setUserService(UserService userService) { this.userService = userService; }
/** * AJAX進(jìn)行異步校驗(yàn)用戶(hù)名的執(zhí)行方法 * * @throws IOException */ public String findByName() throws IOException { User existUser = userService.findByName(user.getUsername());//調(diào)用service層的方法返回?cái)?shù)據(jù)庫(kù)中查詢(xún)出來(lái)的對(duì)象 // 獲得response對(duì)象,向頁(yè)面
效果如下:
以上這篇ajax實(shí)現(xiàn)用戶(hù)名校驗(yàn)的傳統(tǒng)和jquery的$.post方式(實(shí)例講解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com