解决ajax发送请求(POST)参数数据量大的问题 :post请求
解决ajax发送请求参数数据量大的问题
在开发中遇到需要用到ajax从前台传大量数据到后台的的处理操作。需要进行三个方面的改动:(1)前台(2)后台(3)Tomcat。总结要点如下:
一、前台:
1. 使用jQuery提供的 $.ajax()进行多个参数的配置。不再使用$.post()、$.get()、$.getJSON()。
2. 使用post请求,因为get请求后面的参数限制大小。
3. 传的参数使用JSON.stringify()将待传参数序列化成字符串。
以下是前台代码的示例

var data = {linesAll:JSON.stringify(lines)};
var url = webIpServer + "/getVerticalRiverFloodMappingGrid";
//ajax请求示例
$.ajax({
type:"POST",
url:url,
data:data,
// traditional:true,
//contentType:"application/json;charset=utf-8",
//是否异步
async: true,
dataType: "json",
success:function(data) {
console.log(data);
},
error:function (data) {
console.log(data);
}
});
二、后台:
1. 参数设置为String类型,以便映射。
@RequestParam("linesAll")String linesAll
2. @RequestMapping 的两种设置方式
(1)@RequestMapping("/getVerticalRiverFloodMappingGrid")
(2)@RequestMapping(value = "/getVerticalRiverFloodMappingGrid", method = RequestMethod.POST)

@RequestMapping("/getVerticalRiverFloodMappingGrid")
public JsonResult getVerticalRiverFloodMappingGrid(@RequestParam("linesAll")String linesAll){
System.out.println(linesAll);
return JsonResult.success().addInfo("success");
}
三、Tomcat:
tomcat上默认post提交大小为2M,超过这个大小了,就会传值不成功。超过2M修改tomcat配置文件conf下的server.xml中的Connector标签。加入maxPostSize = “xxxx”大小,单位是KB。以下设置的是10M。

<Connector EncodingURI="utf-8" port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxPostSize="10240"/>
数据请求成功:
后台
前台
版权声明:本文为Sonsay原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。