本文主要是介绍function(res)参数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
页面
$.post({url:"checkUsername",data:{username:username //传给服务器的内容},success:function(res){ //res = {data:"开发者服务器返回的内容"}if(res.code == 10000){$("#uSpan").html("用户名存在").css({"color":"green"});}else{$("#uSpan").html("用户名不存在").css({"color":"red"});}}})//服务端resp.getWriter().write(json);
服务端
public class CheckUsernameServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1、获取请求参数String username = req.getParameter("username");//2、调用Service层的业务方法UserService userService = new UserServiceImpl();boolean b = userService.selectUserByName(username);//返回一个公共的数据格式DataResult dataResult;if(b){dataResult = new DataResult(10000,"查询成功");}else{dataResult = new DataResult(20000,"查询失败");}resp.setContentType("application/json;charset=utf-8");ObjectMapper mapper = new ObjectMapper();String json = mapper.writeValueAsString(dataResult);resp.getWriter().write(json);}
这篇关于function(res)参数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!