<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="js/jquery-3.6.0.js"></script> </head> <script> function action(){ $.ajax({ url:"/ajax/logins", type:"get", dataType:"json", data:{"name":"李四", "age": 22}, success:function (result){ //后台响应的是一个Json格式的java字符串,jquery会自动将这个字符串转化为json格式的js对象,简称json对向 console.log(result.code) console.log(result.meg) console.log(result.data.length) // console.log("") for (let i = 0; i < result.data.length; i++) { console.log(result.data[i].name) console.log(result.data[i].age) } }, error:function (){ alert("error") } }); } </script> <body> <p> <input type="button" value="ajax" onclick="action()"> </p> <p> <input type="text" id="uu"> </p> </body> </html>
package com.zzc.servlet; import com.fasterxml.jackson.databind.ObjectMapper; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; public class AjaxServlet1 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding( "utf-8" ); // 不能向ajax响应一个页面,所以text/html不可用application/json resp.setContentType( "application/json;charset=utf-8" ); System.out.println(req.getParameter( "name" )); System.out.println(req.getParameter( "age" )); // System.out.println(req.getParameter( "sex" )); //jackson提供的api ObjectMapper mapper = new ObjectMapper(); User user = new User("张三",88); User user2 = new User("王五",22); User user3= new User("王二麻子",38); User user4 = new User("朱青山岛",438); //将java对象转换成json格式的字符串 ArrayList<User> users = new ArrayList<>(); users.add( user ); users.add( user2 ); users.add( user3 ); users.add( user4 ); R r = new R(); r.setCode( 200 ); r.setMeg( "转换ok" ); r.setData(users); String value = mapper.writeValueAsString( r ); System.out.println( value ); //不能向ajax响应一个页面 PrintWriter out = resp.getWriter(); out.write( value ); out.close(); } }
package com.zzc.servlet; public class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
package com.zzc.servlet; import java.util.List; public class R { private int code; private String meg; private List<User> data; @Override public String toString() { return "R{" + "code=" + code + ", meg='" + meg + '\'' + ", data=" + data + '}'; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMeg() { return meg; } public void setMeg(String meg) { this.meg = meg; } public List<User> getData() { return data; } public void setData(List<User> data) { this.data = data; } }