您好,欢迎来到爱玩科技网。
搜索
您的当前位置:首页JS传递数组到后台

JS传递数组到后台

来源:爱玩科技网

方法一: 
1.使用JSON.stringify 将数组对象转化成json字符串;

var array = ["1", "2"];
$.ajax({  
    type : 'POST',  
    url: path + '/check/testPost', contentType : "application/json" , data : JSON.stringify(array), success : function(data) { } }); 

 

2.传输过程中参数 

3.后台处理

@RequestMapping(value = "/testPost", method = {RequestMethod.POST})
public void testPost(@RequestBody String[] array) throws IOException { for (String string : array) { System.out.println(string); } return ; }

 

 

方法二: 
1.前端不做处理:

var array = ["1", "2"];
$.ajax({  
    type : 'POST', url: path + '/check/testPost', contentType: "application/x-www-form-urlencoded", data: {"array": array}, success : function(data) { } }); 

 

 

 

2.传输过程中参数 

3.后台处理

@RequestMapping(value = "/testPost", method = {RequestMethod.POST})
public void testPost(HttpServletRequest req) throws IOException { String[] array = req.getParameterValues("array[]"); for (String string : array) { System.out.println(string); } return ; }

 

注:两种post请求的content-type不同。

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务