JS使用ajax异步处理json响应数据

来源:互联网 发布:linux下网络配置文件 编辑:程序博客网 时间:2024/04/29 20:16

客户端使用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);    }});

服务器端数据响应代码

@Controllerpublic 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());    }}
原创粉丝点击