//设置response头
HttpServletResponse response = getResponse();
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Expires", "0");
response.setHeader("Content-Type", "text/json;charset=utf-8");
PrintWriter printWriter = response.getWriter();
//获取数据,假设我们需要的数据为WorkerFree,由WorkerFreeJSON负责输出格式
WorkerFree workerFree = workerFreeService.workerFreeById(id);
WorkerFreeJSON wf = new WorkerFreeJSON();
wf.setName(workerFree.getName());
wf.setAge(workerFree.getAge());
wf.setContent(workerFree.getContent());
//记得关闭流
printWriter.write(JSONArray.fromObject(wf).toString());
printWriter.close();
Ajax:使用JSON 进行数据传输
最新推荐文章于 2021-08-05 13:41:18 发布