分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

使用Jquery的Ajax实现用户名实时认证

发布时间:2023-09-06 02:29责任编辑:顾先生关键词:Ajax

Jquery的Ajax学习:

学习Jquery的Ajax完成注册用户名实时认证功能

表单:(使用的Bootstrap框架)

 <form id="login" name = "login"> ???????<div class="row justify-content-md-center"> ???????????<div class="col col-lg-4"> ???????????????<div class="form-group"> ???????????????????<label for="username">用户名:</label> ???????????????????<input type="text" name="username" id="username" class="form-control" placeholder="username"> ???????????????????<p style="color: red" id = "wrong"></p> ???????????????</div> ???????????</div> ???????????<div class="col col-lg-4"> ???????????????<div class="form-group"> ???????????????????<label for="usernames">密码:</label> ???????????????????<input type="password" name="password" id="password" class="form-control" placeholder="password"> ???????????????</div> ???????????</div> ???????</div> ???????<div class="row justify-content-md-center"> ???????????<div class="col col-lg-3"> ???????????????<div class="form-group"> ???????????????????<label for="name">姓名:</label> ???????????????????<input type="text" name="name" id="name" class="form-control" placeholder="username"> ???????????????</div> ???????????</div> ???????????<div class="col col-lg-2"> ???????????????<div class="form-group"> ???????????????????<label for="phone">电话:</label> ???????????????????<input type="text" name="phone" id="phone" class="form-control" placeholder="username"> ???????????????</div> ???????????</div> ???????????<div class="col col-lg-3"> ???????????????<div class="form-group"> ???????????????????<label for="address">地址:</label> ???????????????????<input type="text" name="address" id="address" class="form-control" placeholder="username"> ???????????????</div> ???????????</div> ???????</div> ???????<div class="row justify-content-md-center"> ????????????<div class="col col-lg-2"> ???????????????<button type="button" class="btn btn-primary" onclick="checkLogin()">register</button> ???????????</div> ???????????<div class="col col-lg-1"> ???????????????<button type="reset" class="btn btn-primary">reset</button> ???????????</div> ???????</div> ???</form>

Jquery部分:

<script type="text/javascript"> ???var flag;$(function() { ???????var username; $("input").keyup(function(){ ???????????username = $("#username").val(); ???????????$.ajax({ ???????????????method:‘POST‘, ???????????????async:false, ???????????????url:‘http://localhost:8080/NewTaobao/register‘, ???????????????data:{username:username}, ???????????????success:function(data){ ??????????????????if(data ==‘faild‘){ ???????????????????????flag = false; ??????????????????}else{ ??????????????????flag = true; ??????????????????} ???????????????}, ???????????????error:function(){ ???????????????????alert("连接失败"); ???????????????} ???????????}); ??????????????????????if(flag==false){ ????????????$("#wrong").text("用户名已经存在"); ???????????} ???????????else{ ????????????$("#wrong").text(""); ???????????} ???????});});

 常用属性:

  • method:请求方式(GET/POST)
  • async是否同步(true/false)
  • url:提交的链接
  • data:数据(一般使用较多的为JSON格式)
  • dataType(数据类型)
  • success:成功后执行
  • erro:失败或出错后执行

测试效果:(用户名不在数据库中)

测试结果2:(用户名在数据库中)

数据库信息:

使用Jquery的Ajax实现用户名实时认证

原文地址:https://www.cnblogs.com/PoorGuy/p/10263054.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved