Ajax實(shí)現(xiàn)登錄案例
Ajax登錄案例,供大家參考,具體內(nèi)容如下
Msg
package com.lbl.msg; public class Msg { String msg; int code; public Msg() { } public Msg(String msg, int code) { this.msg = msg; this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } }
RegisterServlet
package com.lbl.servlet; import com.fasterxml.jackson.databind.ObjectMapper; import com.lbl.msg.Msg; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/register") public class RegisterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1:獲取請(qǐng)求發(fā)送的數(shù)據(jù) String username = request.getParameter("username"); response.setContentType("text/html;charset=UTF-8"); //2:判斷用戶名是否注冊(cè) if("abc123".equals(username)){ Msg msg = new Msg("用戶名已經(jīng)注冊(cè)",0); //用戶名已經(jīng)注冊(cè) {"flag":false,"info":"用戶名已經(jīng)注冊(cè)"}; // String jsonStr = "{\"flag\":false,\"info\":\"用戶名已經(jīng)注冊(cè)\"}"; //響應(yīng)回瀏覽器 response.getWriter().write(new ObjectMapper().writeValueAsString(msg)); }else{ Msg msg = new Msg("可以注冊(cè)",1); //用戶名沒(méi)有注冊(cè) // String jsonStr = "{\"flag\":true,\"info\":\"可以注冊(cè)\"}"; //響應(yīng)回瀏覽器 response.getWriter().write(new ObjectMapper().writeValueAsString(msg)); } } }
json_register.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="js/jquery-3.3.1.js"></script> <script type="application/javascript"> $(function () { //1: 給用戶名輸入框綁定失去焦點(diǎn)事件(onfocus onblur) $("#username").on("blur",function () { //獲取用戶名輸入框數(shù)據(jù) var username = $("#username").val(); //2:向服務(wù)器發(fā)出異步請(qǐng)求,讓服務(wù)器去查詢用戶名是否存在 $.post( "register", //表示服務(wù)器的servlet路徑 "username="+username, //表示向服務(wù)器發(fā)送的數(shù)據(jù) function (data) { // msg:用戶名已經(jīng)注冊(cè) code:0 if(data.code==0){ // alert(data.info); $("#spanId").html(data.msg).css("color","green"); }else { $("#spanId").html(data.msg).css("color","red"); } }, "json" ); }); }); </script> </head> <body> <div> <font>會(huì)員注冊(cè)</font>USER REGISTER <form class="form-horizontal" style="margin-top: 5px;"> <table> <tr> <td>用戶名</td> <td> <input type="text" id="username" name="username" placeholder="請(qǐng)輸入用戶名"> <span id="spanId"></span> </td> </tr> <tr> <td>密碼</td> <td> <input type="password" placeholder="請(qǐng)輸入密碼"> </td> </tr> </table> <input type="submit" value="注冊(cè)"/> </form> </div> </body> </html>
運(yùn)行效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 一款經(jīng)典的ajax登錄頁(yè)面 后臺(tái)asp.net
- Ajax異步方式實(shí)現(xiàn)登錄與驗(yàn)證
- ajax 實(shí)現(xiàn)微信網(wǎng)頁(yè)授權(quán)登錄的方法
- ajax實(shí)現(xiàn)登錄功能
- Ajax實(shí)現(xiàn)帶有驗(yàn)證碼的局部刷新登錄界面
- div彈出層的ajax登錄(Jquery版+c#)
- Ajax Session失效跳轉(zhuǎn)登錄頁(yè)面的方法
- ajax編寫簡(jiǎn)單的登錄頁(yè)面
- Ajax實(shí)現(xiàn)漂亮、安全的登錄界面
- 登錄超時(shí)給出提示跳到登錄頁(yè)面(ajax、導(dǎo)入、導(dǎo)出)
相關(guān)文章
深入淺析Nginx實(shí)現(xiàn)AJAX跨域請(qǐng)求問(wèn)題
AJAX從一個(gè)域請(qǐng)求另一個(gè)域會(huì)有跨域的問(wèn)題。那么如何在nginx上實(shí)現(xiàn)ajax跨域請(qǐng)求呢?此問(wèn)題難住很多朋友,下面小編給大家?guī)?lái)了Nginx實(shí)現(xiàn)AJAX跨域請(qǐng)求問(wèn)題的相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧2016-06-06ajax快速解決參數(shù)過(guò)長(zhǎng)無(wú)法提交成功的問(wèn)題
下面小編就為大家?guī)?lái)一篇ajax快速解決參數(shù)過(guò)長(zhǎng)無(wú)法提交成功的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12Ajax跨域問(wèn)題及解決方案(jsonp,cors)
遠(yuǎn)程服務(wù)器上設(shè)法動(dòng)態(tài)的把數(shù)據(jù)裝進(jìn)js格式的文本代碼段中,供客戶端調(diào)用和進(jìn)一步處理。這篇文章主要介紹了ajax跨域問(wèn)題解決方案(jsonp,cors) ,需要的朋友可以參考下2019-06-06通達(dá)OA 使用Ajax和工作流插件實(shí)現(xiàn)根據(jù)人力資源系統(tǒng)數(shù)據(jù)增加OA賬號(hào)(圖文詳解)
這篇文章主要介紹了通達(dá)OA 使用Ajax和工作流插件實(shí)現(xiàn)根據(jù)人力資源系統(tǒng)數(shù)據(jù)增加OA賬號(hào)(圖文詳解),需要的朋友可以參考下2016-12-12SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)
這篇文章主要介紹了SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2016-05-05Ajax的原生實(shí)現(xiàn)關(guān)于MIME類型的使用方法
下面小編就為大家分享一篇Ajax的原生實(shí)現(xiàn)關(guān)于MIME類型的使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03