vue axios 发送的数据太长,报500的错误的解决方案


需求:因为需要同步某一年的所有用户数据,用户数量有1W+,每个用户字段有10+,数据太长,发送请求时,后台无法接收。

网上有方法要修改tomcat配置文件,但这个项目部署在nginx上,可通过修改前台发送的content-type改为json格式发送。正常使用x-www-form-unlencoded格式发送可以参考我另一篇文章。

https://www.cnblogs.com/mogujun/articles/12180459.html

两者在后台接收参数的时候有区别。

使用x-www-form-unlencoded发送时,后台只需要在接口输入参数名字与之对应即可。

使用json发送时,后台需要新建一个与之对应的实体类,用实体类来接收。

 1 Axios({
 2                     method: 'post',
 3                     url: '/xxxxx',
 4                     data:{
 5                         xxx: xxx,
 6                         xxxx: xxxx,
 7                     },
 8                     transformRequest: [
 9                         function (data) {
10                             let ret = ''
11                             for (let it in data) {
12                                 ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
13                             }
14                             ret = ret.substring(0, ret.lastIndexOf('&'));
15                             return ret
16                         }
17                     ],
18                     headers: {
19                         'Content-Type': 'application/x-www-form-urlencoded'
20                     },
21                 }).then((res) => {
22                     console.log(res);
23                 }).catch((err) => {
24                     console.log(err);
25                 })    
通过x-www-form-urlencoded格式发送
 1 Axios({
 2                             method: 'post',
 3                             url: '/xxxxx',
 4                             data:JSON.stringify(response.data),
 5                             headers: {
 6                                 'Content-Type': 'application/json;charset=UTF-8'
 7                             },
 8                         }).then((res) => {
 9                             console.log(res.data);
10                         }).catch((err) => {
11                             console.log(err);
12                         });
通过json格式发送
 1     @RequestMapping("/xxxxx")
 2     @ResponseBody
 3     public String syncStuData(@RequestBody syncDomain syncData) throws Exception{
 4         System.out.println(syncData);
 5         hwAdminService.insertAcademic(syncData.getAcademicList());
 6         hwAdminService.insertClass(syncData.getClassList());
 7         hwAdminService.insertStudent(syncData.getStudentList());
 8 
 9         return "success";
10     }
json格式后台接口

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM