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