Spring boot + Ajax(application/x-www-form-urlencoded 类型),检测用户是否注册
2018-04-10 20:32
513 查看
这应该是最常见的 POST 提交数据的方式了。浏览器的原生 form 表单,如果不设置 enctype 属性,那么最终就会以 application/x-www-form-urlencoded 方式提交数据。 很多时候,我们用 Ajax 提交数据时,也是使用这种方式。例如 JQuery 和 QWrap 的 Ajax,Content-Type 默认值都是「application/x-www-form-urlencoded;charset=utf-8」。 html<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Registory</title><script type="application/javascript" src = "js/usernametest.js"></script></head><body><p>用户注册</p><br><form action="register" method="post">账户:<br><input type="text" name="username" onblur="usernametest()"><span id="msg">请输入用户名</span><br>密码:<br><input type="password" name="password"><br><input type="submit" value="注册"></form></body></html>JSfunction usernamete4000st() {var username = document.getElementsByName("username")[0].value;var xmlhttp;if (window.XMLHttpRequest){xmlhttp = new XMLHttpRequest();} else {xmlhttp = new ActiveXObject();}xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState == 4 && xmlhttp.status == 200){var msg = document.getElementById("msg");if (xmlhttp.responseText == "true"){msg.innerHTML =" 用户名可用";}else {msg.innerHTML = "用户名已注册";}}}xmlhttp.open("POST", "/register/usernametest", true);xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlhttp.send("username=" + username);}Spring boot@RequestMapping("/register/usernametest")public String userNameTest(@RequestParam("username") String username){System.out.println(username);User user = userRepository.findByUserName(username);if (user == null){return "true";} else {return "false";}}这里要注意js中的username取值,,因为getElementById返回的数组,所以得加下标,不然控制器的username会接收到undefined字符串。
相关文章推荐
- ajax中的application/x-www-form-urlencoded中的使用
- ajax中的application/x-www-form-urlencoded中的使用
- ajax实现检测当前注册用户是否存在
- ajax中的application/x-www-form-urlencoded中的使用
- jquery与php交互的ajax应用第一课:检测用户注册时用户名是否存在
- ajax中的application/x-www-form-urlencoded中的使用
- Ajax检测注册用户是否存在
- ajax中的application/x-www-form-urlencoded中的使用[转]
- ajax中的application/x-www-form-urlencoded中的使用
- 毕业设计(十)---用DWR实现 代替AJAX 用户注册检测用户名是否已经存在
- Ajax检测注册用户是否存在
- 毕业设计(十)---用DWR实现 代替AJAX 用户注册检测用户名是否已经存在
- jquery与php交互的ajax应用第一课:检测用户注册时用户名是否存在
- jquery与php交互的ajax应用第一课:检测用户注册时用户名是否存在
- Ajax检测注册用户是否存在
- SSH框架中用Ajax和jquery检测用户注册名是否重复
- ajax中的application/x-www-form-urlencoded中的使用(转载)
- Ajax检测注册用户是否存在
- ajax中的application/x-www-form-urlencoded中的使用
- MVC3学习:利用mvc3+ajax检测用户是否被注册