功能描述:当输入框失去焦点,发起ajax请求到后端
1. 页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<%--引入jquery--%>
<script src="${pageContext.request.contextPath}/static/js/jquery-3.0.0.min.js"></script>
<script>
function a() {
$.post({
url:"${pageContext.request.contextPath}/a1",
data:{"name":$("#username").val()},
success:function(data){
alert(data);
}
})
}
</script>
<body>
<div>
<input id="username" onblur="a()" type="text"/>
</div>
</body>
</html>
2. Controller
package com.hzu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
public class AjaxController {
@RequestMapping("/a1")
public void a1(String name, HttpServletResponse response) throws IOException {
System.out.println("name==>"+name);
if(name.equals("TOP")){
response.getWriter().print("true");
}else {
response.getWriter().print("false");
}
}
}