axios request requestBody and formData

Foreword

Management Development in the background of vue, we should demand, the need for information to do a check, you need to pass two parameters of the past, to prevent a body in a prevent formdata in, axios request will default parameter in formdata in transmission.
Just the same front-end, it is nothing more than the format parameters of the problem.
For the back-end
(Form data) can be request.getParameter (reception parameter name)
(Request payload) with request.getParameter is not receiving values ​​must be acquired input stream, in turn resulting string json
Should demand some interfaces are required to be placed on the body in some formdata in the front end of a flexible process needs to be done, because even just change the headers in the field people will always remember. So eventually I will request the file package are as follows:
  1. /**
  2. Network configuration request * @description
  3. * @Author luokaibin chaizhiyang
  4. */
  5. import axios from 'axios'
  6. import { Message} from 'element-ui'
  7. import router from '../router/permission'
  8. Import view from 'View'
  9. Import VueCookies from 'sight-cookies
  10. const moment = require('moment');
  11. const Base64 = require('js-base64').Base64;
  12. // loading setting the partial refresh frame, and closed after loading block all requests complete
  13. var loading ;
  14. function startLoading() {
  15. loading = Vue.prototype.$loading({
  16. lock: true,
  17. text: "Loading...",
  18. target : Document . querySelector ( '.loading-Area' ) // set the animation loading area
  19. });
  20. }
  21. function endLoading() {
  22. loading.close();
  23. }
  24. // declare an object for storing the number of requests
  25. var needLoadingRequestCount = 0 ;
  26. function showFullScreenLoading() {
  27. if ( needLoadingRequestCount === 0 ) {
  28. startLoading();
  29. }
  30. needLoadingRequestCount ++ ;
  31. };
  32. function tryHideFullScreenLoading() {
  33. if ( needLoadingRequestCount <= 0 ) return ;
  34. needLoadingRequestCount - ;
  35. if ( needLoadingRequestCount === 0 ) {
  36. endLoading();
  37. }
  38. };
  39. // request interception
  40. axios.interceptors.request.use(config => {
  41. let token = "";
  42. showFullScreenLoading();
  43. if(VueCookies.isKey('userinfo')) {
  44. const USERINFO = VueCookies.get('userinfo');
  45. if(config.method == 'get') {
  46. token = Base64.encode(USERINFO.token + '|' + moment().utc().format('YYY-MM-DD HH:mm:ss' + '|' + JSON.stringify(config.params)));
  47. config.params.hospitalId = USERINFO.hospitalId;
  48. } else {
  49. token = Base64.encode(USERINFO.token + '|' + moment().utc().format('YYY-MM-DD HH:mm:ss' + '|' + JSON.stringify(config.data)));
  50. config.data.hospitalId = USERINFO.hospitalId;
  51. }
  52. let TOKENSTART = token.slice(0,10),
  53. TOKENEND = token.slice(10);
  54. token = TOKENEND + TOKENSTART;
  55. config.headers['token'] = token;
  56. }
  57. return config;
  58. }, err => {
  59. tryHideFullScreenLoading();
  60. The Message . Error ({ Message : 'Request Timeout!' });
  61. return Promise.resolve(err);
  62. })
  63. // response interception
  64. axios.interceptors.response.use(res => {
  65. tryHideFullScreenLoading();
  66. switch(res.data.code) {
  67. case 200:
  68. return res.data.result;
  69. case 401:
  70. router.push('/login');
  71. VueCookies.remove('userinfo');
  72. return Promise.reject(res);
  73. case 201:
  74. Message.error({ message: res.data.msg });
  75. returnPromise.reject(res);
  76. default :
  77. returnPromise.reject(res);
  78. }
  79. }, err => {
  80. tryHideFullScreenLoading();
  81. if(!err.response.status) {
  82. return false;
  83. }
  84. switch(err.response.status) {
  85. case 504:
  86. The Message . Error ({ Message : 'server is eaten ⊙﹏⊙∥' });
  87. break;
  88. case 404:
  89. The Message . Error ({ Message : 'server is eaten ⊙﹏⊙∥' });
  90. break;
  91. case 403:
  92. The Message . Error ({ the Message : 'insufficient permissions, contact your administrator!' });
  93. break;
  94. default:
  95. return Promise.reject(err);
  96. }
  97. })
  98. Axios . Defaults . timeout = 300000 ; // Request Timeout 5fen // requestBody
  99. export const postJsonRequest = (url, params) => {
  100. return axios ({
  101. method: 'post',
  102. url : url ,
  103. data: params,
  104. headers: {
  105. 'Content-Type': 'application/json',
  106. },
  107. });
  108. }
    // formdat A
  109. export const postRequest = (url, params) => {
  110. return axios ({
  111. method:'post',
  112. url : url ,
  113. data:params,
  114. transformRequest: [function (data) {
  115. easy right = ''
  116. for (let it in data) {
  117. ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  118. }
  119. return right
  120. }],
  121. headers: {
  122. 'Content-Type': 'application/x-www-form-urlencoded'
  123. }
  124. });
  125. }
  126. export const getRequest = (url, data = '') => {
  127. return axios ({
  128. method: 'get',
  129. params: data,
  130. url : url ,
  131. });
  132. }
Get request, then do not need to be set, since requests get back to the default parameters in params in, post the request, then there will be two, so we are here to talk about post encapsulates the request in duplicate.
 
 
Lead to https://www.cnblogs.com/bgwhite/p/9964864.html

Guess you like

Origin www.cnblogs.com/braveLN/p/11199862.html