ajax技术前后台(mvc)

来源:互联网 发布:博世待遇怎么样 知乎 编辑:程序博客网 时间:2024/06/05 11:28

html

<div class="form-group required"><label class="col-sm-2 control-label">姓名:</label><div class="col-sm-6"><form:input id="name" path="name" htmlEscape="false" maxlength="100" class="form-control required" readonly=""onchange="change(this.value)" /></div><span id = "msg"></span></div>
javascript

function change(name){if(name != null && name != ""){alert("调用方法!");checkname(name);}}function checkname(name){$.ajax({url:"${ctx}/*****/*****/*****",type:"post",dataType:"JSON",data:{name:name},success:function(result){//已经存在该姓名if(result == true){// alert("result:"+result);$("#msg").html("该账号已经存在!");}else{// alert("result:"+result);$("#msg").html("可以使用");}},error:function(){alert("name:"+name);alertx("检查输入是否发生错误。");}})}
controller

@RequestMapping(value = "*****")public String ifnow(User user,HttpServletResponse response) {List<User> list = UserService.findNameRepeat(User);if(list.size()>0){log.info("已经存在!");return stringpage(response, "false", "");}else{log.info("可以使用!");return stringpage(response, "true", "");}}
common

protected String stringpage(HttpServletResponse response, String string, String type) {try {response.reset();        response.setContentType(type);        response.setCharacterEncoding("utf-8");response.getWriter().print(string);return null;} catch (IOException e) {return null;}}

时间仓促,整理一下,方便以后查看使用,其中还有不少误区,但基本通讯实现了,请在此基础上修改即可,
有好的建议可以留言一起讨论!



0 0