通过Ajax转入数组,后端通过@RequestBody接收
1、前端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns="http://www.w3.org/1999/xhtml"> < head > < meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < title >清风</ title > < link type="text/css" href="css/css.css" rel="stylesheet" /> < script type="text/javascript" src="js/jquery.js"></ script > < script type="text/javascript" src="js/js.js"></ script > < script type="text/javascript" src="js/restfull.js"></ script > </ head > < body style="background: #FFF;"> < button id="btn1">Send [5,8,12] One</ button > <!-- 页面结束--> < script type="text/javascript"> $("#btn1").click(function(){ // 准备好要发送到服务器端的数组 var array = [5, 8, 12]; // 将JSON数组转换为JSON字符串 var requestBody = JSON.stringify(array); var url = restapi.url_api_e17+"user/array"; $.ajax({ "url": url, // 请求目标资源的地址 "type": "post", // 请求方式 "data": requestBody, // 请求的参数 "contentType": "application/json;charset=UTF-8", // 设置请求体的内容类型,告诉服务器端本次请求的请求体是JSON数据 "dataType": "json", // 如服务器端返回的数据类型json "success": function(response) { // 服务器端成功处理请求后调用的回调函数,response是响应体数据 if(response.status == 200){ alert("请求成功"); }else{ alert("请求失败"); } }, "error":function(response) { // 服务器端处理请求失败后调用的回调函数,response是响应体数据 alert(response); } }); }); } </ script > </ body > </ html > |
2、后端代码
1 2 3 4 5 6 7 8 | @ApiOperation("接收参数数组") @PostMapping("/array") public Result array( @RequestBody List< Integer > array){ for (Integer arr : array) { log.info("遍历的数组:"+arr); } return Result.ok(); } |
3、输出结果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南