客户端使用ajax异步请求服务器
$.ajax({
async: true,
type: "post",
url: "getcode",
dataType: "json",
success: function(data){
data = data.toString();
$(".code").val(data);
},
error: function(xhr){
alert("服务器环境异常-->"+xhr.status);
}
});
服务器端数据响应代码
@Controller
public class Controller{
@RequestMapping("getcode")
public void getCode(HttpServletRequest request,HttpServletResponse response){
UUID uuid = uuid.randUUID();
String code = uuid.toString().replace("-","");
HttpSession session = reqeust.getSession();
session.setAttribute("code",code);
PrintWriter writer = response.getWriter();
JSONArray jsonarr = new JSONArray();
jsonarr.put(code);
writer.print(jsonarr.toString());
}
}