我是靠谱客的博主 长情帅哥,这篇文章主要介绍页面ajax请求传参及java后端数据接收,现在分享给大家,希望可以做个参考。

js ajax请求传参及java后端数据接收

复制代码
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
1 Controller: 2 package com.ysl.PassingParameters.controller; 3 import java.util.List; 4 import java.util.Map; 5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestBody; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestParam; 10 import org.springframework.web.bind.annotation.ResponseBody; 11 12 import com.ysl.PassingParameters.bean.User; 13 import com.ysl.PassingParameters.dto.RetMsg; 14 15 @Controller 16 public class TestController { 17 18 /** 19 * List<String>传参 20 * @param listString 21 * @return 22 */ 23 @RequestMapping("/listString") 24 @ResponseBody 25 public RetMsg listString(@RequestParam("listString[]") List<String> listString){ 26 System.out.println("listString:"+listString.toString()); 27 return RetMsg.success(); 28 } 29 30 31 /** 32 * List<User>传参 33 * @param listUser 34 * @return 35 */ 36 @RequestMapping("/listUsers") 37 @ResponseBody 38 public RetMsg listUsers(@RequestBody List<User> listUser){ 39 System.out.println("username:"+listUser.get(0).getUsername()); 40 return RetMsg.success(); 41 } 42 43 44 /** 45 * User[]传参 46 * @param arrayUsers 47 * @return 48 */ 49 @RequestMapping("/arrayUsers") 50 @ResponseBody 51 public RetMsg arrayUsers(@RequestBody User[] arrayUsers){ 52 System.out.println("username:"+arrayUsers[0].getUsername()); 53 return RetMsg.success(); 54 } 55 56 57 /** 58 * List<Map<String,Object>>传参 59 * @param listMap 60 * @return 61 */ 62 @RequestMapping("/listMap") 63 @ResponseBody 64 public RetMsg listMap(@RequestBody List<Map<String, String>> listMap){ 65 System.out.println("username:"+listMap.get(0).get("username")); 66 return RetMsg.success(); 67 } 68 69 /** 70 * User对象传参 71 * @param arrayUsers 72 * @return 73 */ 74 @RequestMapping("/users") 75 @ResponseBody 76 public RetMsg users(@RequestBody User users){ 77 System.out.println("username:"+users.getUsername()); 78 System.out.println("username:"+users.getList().get(0).getUsername()); 79 return RetMsg.success(); 80 } 81 } 82 页面: 83 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> 84 <html> 85 <%application.setAttribute("path", request.getContextPath());%> 86 <head> 87 <script type="text/javascript" src="${path}/js/jquery.min.js"></script> 88 </head> 89 <body> 90 <h2>Hello World!</h2> 91 <button id="listString">List&lt;String&gt;传参</button> 92 <button id="listUser">List&lt;User&gt;传参</button> 93 <button id="arrayUsers">User[]传参</button> 94 <button id="listMap">List&lt;Map&lt;String,Object&gt;&gt;传参</button> 95 <button id="Users">User对象(属性包含List&lt;User&gt;)传参</button> 96 <script type="text/javascript"> 97 98 // List<String>传参 99 $("#listString").click(function(){ 100 var idList = new Array(); 101 idList.push("1"); 102 idList.push("1"); 103 idList.push("1"); 104 $.ajax({ 105 type:"post", 106 url:"${path}/listString", 107 data:{"listString":idList}, 108 dataType:"json", 109 success:function(retMsg){ 110 if(retMsg.code==200){ 111 alert("success"); 112 }else{ 113 alert("false"); 114 } 115 } 116 }) 117 }) 118 119 // List<User>传参 120 $("#listUser").click(function(){ 121 var userList = new Array(); 122 userList.push({username: "zhangsan",password: "332"}); 123 userList.push({username: "zhangsan",password: "332"}); 124 $.ajax({ 125 type:"post", 126 url:"${path}/listUsers", 127 data:JSON.stringify(userList), 128 dataType:"json", 129 contentType : 'application/json;charset=utf-8', //设置请求头信息 130 success:function(retMsg){ 131 if(retMsg.code==200){ 132 alert("success"); 133 }else{ 134 alert("false"); 135 } 136 } 137 }) 138 }) 139 140 141 //传User对象数组 142 $("#arrayUsers").click(function(){ 143 var userList = [{username: "李四",password: "123"},{username: "张三",password: "332"}]; 144 $.ajax({ 145 type: "POST", 146 url: "${path}/arrayUsers", 147 data: JSON.stringify(userList),//将对象序列化成JSON字符串 148 dataType:"json", 149 contentType : 'application/json;charset=utf-8', //设置请求头信息 150 success:function(retMsg){ 151 if(retMsg.code==200){ 152 alert("success"); 153 }else{ 154 alert("false"); 155 } 156 } 157 }); 158 }) 159 160 // List<Map<String,Object>>传参 161 $("#listMap").click(function(){ 162 var userList = new Array(); 163 userList.push({username: "zhangsan",password: "332"}); 164 userList.push({username: "zhangsan",password: "332"}); 165 $.ajax({ 166 type:"post", 167 url:"${path}/listMap", 168 data:JSON.stringify(userList), 169 dataType:"json", 170 contentType : 'application/json;charset=utf-8', //设置请求头信息 171 success:function(retMsg){ 172 if(retMsg.code==200){ 173 alert("success"); 174 }else{ 175 alert("false"); 176 } 177 } 178 }) 179 }) 180 181 //User对象传参 182 $("#Users").click(function(){ 183 var list = new Array(); 184 list.push({username: "zhangsan",password: "332"}); 185 list.push({username: "zhangsan",password: "332"}); 186 var user = {}; 187 user.username = "张三"; 188 user.password = "密码"; 189 user.list = list; 190 $.ajax({ 191 type:"post", 192 url:"users", 193 data:JSON.stringify(user), 194 datatype:"json", 195 contentType:"application/json;charset=utf-8", 196 success:function(retMsg){ 197 if(retMsg.code==200){ 198 alert("success"); 199 }else{ 200 alert("false"); 201 } 202 } 203 }) 204 }) 205 </script> 206 </body> 207 </html>

 

@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);

GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。

https://blog.csdn.net/justry_deng/article/details/80972817

 

转载于:https://www.cnblogs.com/new-life/p/11138873.html

最后

以上就是长情帅哥最近收集整理的关于页面ajax请求传参及java后端数据接收的全部内容,更多相关页面ajax请求传参及java后端数据接收内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(97)

评论列表共有 0 条评论

立即
投稿
返回
顶部