第一種:傳統(tǒng)的ajax異步請(qǐng)求,后臺(tái)代碼以及效果在最下邊
首先我們?cè)趀clipse中創(chuàng)建一個(gè)注冊(cè)頁面regist.jsp,創(chuàng)建一個(gè)form表單,注意,由于我們只是實(shí)現(xiàn)用戶名校驗(yàn)的效果,下邊紅色部門是我們需要研究對(duì)象,所以其他的部門可以忽略不看。
內(nèi)容如下:
%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> !DOCTYPE html> html> head> meta charset="UTF-8"> title>用戶注冊(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)求用戶名是否存在 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)聽 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.打開連接 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è)用戶名input輸入框,并添加一個(gè)onblur="checkUsername()"事件 div class="username">用nbsp;nbsp;戶nbsp;nbsp;名:nbsp;nbsp;input class="shurukuang" type="text" id="username" name="username" onblur="checkUsername()"/>span id="span">/span>/div> div class="username">密nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;碼:nbsp;nbsp;input class="shurukuang" type="password" id="password" name="password"/>/div> div class="username">確認(rèn)nbsp;密碼:nbsp;input class="shurukuang" type="password" id="repassword" name="repassword" />/div> div class="username">郵nbsp;nbsp;箱nbsp;nbsp;號(hào):nbsp;nbsp;input class="shurukuang" type="email" id="email" name="email" />/div> div class="username">姓nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;名:nbsp;nbsp;input class="shurukuang" type="text" id="name" name="name"/>/div> div class="username">手nbsp;nbsp;機(jī)nbsp;nbsp;號(hào):nbsp;nbsp;input class="shurukuang" type="text" id="phone" name="phone"/>/div> div class="username">地nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;址:nbsp;nbsp;input class="shurukuang" type="text" id="addr" name="addr"/>/div> div class="username"> div class="left fl">驗(yàn)nbsp;nbsp;證nbsp;nbsp;碼:nbsp;nbsp;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)以上效果。首先form表單以及Action中的都不變,我們只需改變script就可以了。
第一步:引入js文件script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-3.2.1.min.js">/script>
第二步:
//ajax異步請(qǐng)求用戶名是否存在 $(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中 }); } }); })
然后我們來看一下后臺(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 ModelDrivenUser> { 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)用戶名的執(zhí)行方法 * * @throws IOException */ public String findByName() throws IOException { User existUser = userService.findByName(user.getUsername());//調(diào)用service層的方法返回?cái)?shù)據(jù)庫中查詢出來的對(duì)象 // 獲得response對(duì)象,向頁面輸出: HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8");//設(shè)置編碼格式 // 判斷返回的對(duì)象是否為空 if (existUser != null) { // 如果有,查詢到該用戶:用戶名已經(jīng)存在 response.getWriter().println("用戶名已經(jīng)存在"); } else { // 如果沒有,用戶名可以使用 response.getWriter().println("font color='green'>用戶名可以使用/font>"); } return NONE;//此處返回空 }
效果如下:
以上這篇ajax實(shí)現(xiàn)用戶名校驗(yàn)的傳統(tǒng)和jquery的$.post方式(實(shí)例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
標(biāo)簽:本溪 內(nèi)江 營口 四川 玉樹 遼寧 益陽 銅川
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ajax實(shí)現(xiàn)用戶名校驗(yàn)的傳統(tǒng)和jquery的$.post方式(實(shí)例講解)》,本文關(guān)鍵詞 ajax,實(shí),現(xiàn)用,戶名,校驗(yàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。