ajax请求返回json数据

在这里插入图片描述
servlet:

@WebServlet("/provinceServlet")
public class ProvinceServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.调用service查询
        ProvinceService service = new ProvinceServiceImpl();
        //2.序列化list为json
        List list = service.findAll();
        //3.响应结果
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(list);
        System.out.println(json);

        //3.相应结果
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

在这里插入图片描述

ajax请求servlet

  
    

ajax请求返回json数据_第1张图片

你可能感兴趣的:(jQuery)