这是前端的代码,就是一个原生ajax,然后把它绑定到一个按钮上
userExistErrorSpan = document.getElementById("userExistError");
function doUserExist(){
//使用ajax判断用户是否已存在
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
userExistErrorSpan.innerHTML = xmlHttp.responseText;
}
}
var param = "phoneNumber=" + document.getElementById("phoneNumber").value;
xmlHttp.open("get", "ifUserExist?" + param, true);
xmlHttp.setRequestHeader("Content-type", "text/plain;charset=utf-8");
xmlHttp.send();
}
这是控制层的方法:
@RequestMapping(value = "ifUserExist", method= RequestMethod.GET)
public void ifUserExist(HttpServletResponse response, String phoneNumber) throws IOException {
if(phoneNumber != null){
System.out.println("接受了Ajax请求");
if(phoneNumber.length() == 11){
PrintWriter out = response.getWriter();
Long phoneNumberConvert = Long.valueOf(phoneNumber);
response.setContentType("text/html;charset=utf-8");
int num = service.userExist(phoneNumberConvert);
if(num > 0){
out.print("该用户已存在!请直接<a href = 'loginPage.jsp'>登录</a>");
out.flush();
out.close();
}
}
}
}
但是这个请求是失败的,会出现HTTP500错误。然后后端也会有一些错误语句。
后端控制台的错误:
接受了Ajax请求
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@55d02880] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.jdbc.JDBC4Connection@48b6bc9c] will not be managed by Spring
==> Preparing: select count(*) from siteUsersInfo where phoneNumber = ?
==> Parameters: 13490645764(Long)
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@55d02880]
前端的错误: