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

 Controller:
package com.ysl.PassingParameters.controller;
import java.util.List;
import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import com.ysl.PassingParameters.bean.User;
import com.ysl.PassingParameters.dto.RetMsg; @Controller
public class TestController { /**
* List<String>传参
* @param listString
* @return
*/
@RequestMapping("/listString")
@ResponseBody
public RetMsg listString(@RequestParam("listString[]") List<String> listString){
System.out.println("listString:"+listString.toString());
return RetMsg.success();
} /**
* List<User>传参
* @param listUser
* @return
*/
@RequestMapping("/listUsers")
@ResponseBody
public RetMsg listUsers(@RequestBody List<User> listUser){
System.out.println("username:"+listUser.get(0).getUsername());
return RetMsg.success();
} /**
* User[]传参
* @param arrayUsers
* @return
*/
@RequestMapping("/arrayUsers")
@ResponseBody
public RetMsg arrayUsers(@RequestBody User[] arrayUsers){
System.out.println("username:"+arrayUsers[0].getUsername());
return RetMsg.success();
} /**
* List<Map<String,Object>>传参
* @param listMap
* @return
*/
@RequestMapping("/listMap")
@ResponseBody
public RetMsg listMap(@RequestBody List<Map<String, String>> listMap){
System.out.println("username:"+listMap.get(0).get("username"));
return RetMsg.success();
} /**
* User对象传参
* @param arrayUsers
* @return
*/
@RequestMapping("/users")
@ResponseBody
public RetMsg users(@RequestBody User users){
System.out.println("username:"+users.getUsername());
System.out.println("username:"+users.getList().get(0).getUsername());
return RetMsg.success();
}
}
页面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<%application.setAttribute("path", request.getContextPath());%>
<head>
<script type="text/javascript" src="${path}/js/jquery.min.js"></script>
</head>
<body>
<h2>Hello World!</h2>
<button id="listString">List&lt;String&gt;传参</button>
<button id="listUser">List&lt;User&gt;传参</button>
<button id="arrayUsers">User[]传参</button>
<button id="listMap">List&lt;Map&lt;String,Object&gt;&gt;传参</button>
<button id="Users">User对象(属性包含List&lt;User&gt;)传参</button>
<script type="text/javascript"> // List<String>传参
$("#listString").click(function(){
var idList = new Array();
idList.push("1");
idList.push("1");
idList.push("1");
$.ajax({
type:"post",
url:"${path}/listString",
data:{"listString":idList},
dataType:"json",
success:function(retMsg){
if(retMsg.code==200){
alert("success");
}else{
alert("false");
}
}
})
}) // List<User>传参
$("#listUser").click(function(){
var userList = new Array();
userList.push({username: "zhangsan",password: "332"});
userList.push({username: "zhangsan",password: "332"});
$.ajax({
type:"post",
url:"${path}/listUsers",
data:JSON.stringify(userList),
dataType:"json",
contentType : 'application/json;charset=utf-8', //设置请求头信息
success:function(retMsg){
if(retMsg.code==200){
alert("success");
}else{
alert("false");
}
}
})
}) //传User对象数组
$("#arrayUsers").click(function(){
var userList = [{username: "李四",password: "123"},{username: "张三",password: "332"}];
$.ajax({
type: "POST",
url: "${path}/arrayUsers",
data: JSON.stringify(userList),//将对象序列化成JSON字符串
dataType:"json",
contentType : 'application/json;charset=utf-8', //设置请求头信息
success:function(retMsg){
if(retMsg.code==200){
alert("success");
}else{
alert("false");
}
}
});
}) // List<Map<String,Object>>传参
$("#listMap").click(function(){
var userList = new Array();
userList.push({username: "zhangsan",password: "332"});
userList.push({username: "zhangsan",password: "332"});
$.ajax({
type:"post",
url:"${path}/listMap",
data:JSON.stringify(userList),
dataType:"json",
contentType : 'application/json;charset=utf-8', //设置请求头信息
success:function(retMsg){
if(retMsg.code==200){
alert("success");
}else{
alert("false");
}
}
})
}) //User对象传参
$("#Users").click(function(){
var list = new Array();
list.push({username: "zhangsan",password: "332"});
list.push({username: "zhangsan",password: "332"});
var user = {};
user.username = "张三";
user.password = "密码";
user.list = list;
$.ajax({
type:"post",
url:"users",
data:JSON.stringify(user),
datatype:"json",
contentType:"application/json;charset=utf-8",
success:function(retMsg){
if(retMsg.code==200){
alert("success");
}else{
alert("false");
}
}
})
})
</script>
</body>
</html>

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

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

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

页面ajax请求传参及java后端数据接收的更多相关文章

  1. 前端集合传参,springmvc后端如何接收

    废话不多说,上代码 后端接收对象: class ObjectA{ private String a; private String b; private List<ObjectB> lis ...

  2. 小程序页面跳转传参-this和that的区别-登录流程-下拉菜单-实现画布自适应各种手机尺寸

    小程序页面跳转传参 根目录下的 app.json 文件 页面文件的路径.窗口表现.设置网络超时时间.设置多 tab { "pages": [ "pages/index/i ...

  3. [转]ASP.NET MVC学习系列(二)-WebAPI请求 传参

    [转]ASP.NET MVC学习系列(二)-WebAPI请求 传参 本文转自:http://www.cnblogs.com/babycool/p/3922738.html ASP.NET MVC学习系 ...

  4. ajax请求传过来的json数据直接转成对应的实体类时出错:400 Bad Request 不进入controller

    今天开发过程中,在SpringMVC中的Action中处理前台ajax请求传过来的json数据直接转成对应的实体类时出错:400 Bad Request,后台也不报错,400指的的是请求无效(请求有语 ...

  5. 13.scrapy框架的日志等级和请求传参

    今日概要 日志等级 请求传参 如何提高scrapy的爬取效率 今日详情 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是s ...

  6. scrapy框架的日志等级和请求传参

    日志等级 请求传参 如何提高scrapy的爬取效率 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息 ...

  7. scrapy框架之日志等级和请求传参-cookie-代理

    一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息. - 日志信息的种类: ERROR : 一般错误 ...

  8. 爬虫--Scrapy-参数等级和请求传参

    日志等级 日志等级(种类): ERROR:错误 WARNING:警告 INFO:一般信息 DEBUG:调试信息(默认) 指定输入某一中日志信息: settings:LOG_LEVEL = ‘ERROR ...

  9. Scrapy的日志等级和请求传参

    日志等级 日志信息:   使用命令:scrapy crawl 爬虫文件 运行程序时,在终端输出的就是日志信息: 日志信息的种类: ERROR:一般错误: WARNING:警告: INFO:一般的信息: ...

随机推荐

  1. Java数据结构ArrayList

    Java数据结构ArrayList /** * <html> * <body> * <P> Copyright JasonInternational</p&g ...

  2. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 动态数据的呈现

    https://www.cnblogs.com/cynchanpin/p/7065098.html 在MVC3開始.视图数据能够通过ViewBag属性訪问.在MVC2中则是使用ViewData.MVC ...

  3. 在函数作用域嵌套下使用this

    var myObj = {    specialFunction: function () {        console.log("specialFunction.");   ...

  4. springboot启动流程(六)ioc容器刷新前prepareContext

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html prepareContext方法核心逻辑 上一篇文章中,我们通过createApplicati ...

  5. 在textarea和input光标处插入内容,支持ie

    项目需求,用户要能够输入和点击外面的公式去插入到textaera中,试了好几种方法,有的是在谷歌下好使,在ie下不好使,最后找到了下面这个方法,目前在ie8以上都可以生效.直接上代码 function ...

  6. LeetCode 【2】 Reverse Integer --007

    六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...

  7. SQL性能优化思路

    1. 尽可能把数据的存储和计算放入Memory而不是Disk,且减少IO操作,比如运用Redis等缓存技术 2. 对数据表进行精心设计,特别是大数据表,对常用数据字段进行适当的冗余,尽可能避免分表导致 ...

  8. GPU显存释放

    一.当程序没有运行,但GPU仍被占用, 可通过nvidia-smi查看,被占用的pid是什么 或通过sudo fuser -v /dev/nvidia* #查找占用GPU资源的PID 然后采用kill ...

  9. 计算广告(4)----搜索广告召回(也叫match、触发)

    一.搜索广告形态 1.特征工程 特征主要有用户画像(user profile).用户行为(user behavior).广告(ad)和上下文(context)四部分组成,如下所示: 2.平台算法主要分 ...

  10. 能用的单纯形法python代码

    网上找了一些代码,发现有一些是不能用的,出现错误说集合为空 1.网上出现了好多次,但是不能用的,只能部分模型能用,比如例子中所示 原链接:https://www.jianshu.com/p/b233c ...