Servlet接收JS中$.post方法传来的数组、对象问题解决方案 左手的ㄟ右手 2022-05-10 07:40 54阅读 0赞 ## Servlet接收数组 ## 要在ajax什么加个$.ajaxSettings.traditional=true;才可以传数组 var goods=["11","11"]; $.ajaxSettings.traditional=true; $.post("postObject",{goods:goods},function (data) { }); Servlet通过getParameterValues方法接收 @WebServlet(name = "PostObjectServlet",urlPatterns = {"/postObject"}) public class PostObjectServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] str=request.getParameterValues("goods"); System.out.println(str); System.out.println(str.length); for(String string: str){ System.out.println(string); } } ## Servlet接收对象 ## var foods=[{name:"cool",age:1},{name:"cool",age:1}] $.ajaxSettings.traditional=true; $.post("postObject",{foods:foods},function (data) { }); 暂时没有实现接收
还没有评论,来说两句吧...