三种post 提交数据方式

在Axios中可以通过设置Content-Type来改变数据改变方式, 有时在前端提交数据时会发现后端SpringMVC接收不到数据的情况, 因此可以尝试改变提交方式传递数据.

比如在Postman中, 就可以看到这三种提交方式:
1, form-data
2, x-www-form-urlencoded
3, row - json
image.png

这里用https://github.com/PanJiaChen/vue-admin-template/blob/master/src/utils/request.js 该文件封装了axios的方法, 只需要调用service 实例即可

  1. import axios from 'axios'
  2. import { MessageBox, Message } from 'element-ui'
  3. import store from '@/store'
  4. import { getToken } from '@/utils/auth'
  5. // create an axios instance
  6. const service = axios.create({
  7. baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  8. // withCredentials: true, // send cookies when cross-domain requests
  9. timeout: 5000 // request timeout
  10. })
  11. // request interceptor
  12. service.interceptors.request.use(
  13. config => {
  14. // do something before request is sent
  15. if (store.getters.token) {
  16. // let each request carry token
  17. // ['X-Token'] is a custom headers key
  18. // please modify it according to the actual situation
  19. config.headers['X-Token'] = getToken()
  20. }
  21. return config
  22. },
  23. error => {
  24. // do something with request error
  25. console.log(error) // for debug
  26. return Promise.reject(error)
  27. }
  28. )
  29. // response interceptor
  30. service.interceptors.response.use(
  31. /**
  32. * If you want to get http information such as headers or status
  33. * Please return response => response
  34. */
  35. /**
  36. * Determine the request status by custom code
  37. * Here is just an example
  38. * You can also judge the status by HTTP Status Code
  39. */
  40. response => {
  41. const res = response.data
  42. // if the custom code is not 20000, it is judged as an error.
  43. if (res.code !== 20000) {
  44. Message({
  45. message: res.message || 'Error',
  46. type: 'error',
  47. duration: 5 * 1000
  48. })
  49. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  50. if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
  51. // to re-login
  52. MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
  53. confirmButtonText: 'Re-Login',
  54. cancelButtonText: 'Cancel',
  55. type: 'warning'
  56. }).then(() => {
  57. store.dispatch('user/resetToken').then(() => {
  58. location.reload()
  59. })
  60. })
  61. }
  62. return Promise.reject(new Error(res.message || 'Error'))
  63. } else {
  64. return res
  65. }
  66. },
  67. error => {
  68. console.log('err' + error) // for debug
  69. Message({
  70. message: error.message,
  71. type: 'error',
  72. duration: 5 * 1000
  73. })
  74. return Promise.reject(error)
  75. }
  76. )
  77. export default service

项目的代码来实现这三种方式:

1、Content-Type: application/json

  1. import service from '@/utils/request'
  2. export default {
  3. test() {
  4. return service({ // Promise
  5. url:'http://localhost:8080/dev-api/getMockPersonInfo',
  6. method: 'post',
  7. data:{
  8. "name": "王静飞",
  9. "pagesize":"20",
  10. "curpage":"1"
  11. }
  12. })
  13. }
  14. }

Postman 测试: 传递Json
image.png

image.png

2、Content-Type: multipart/form-data

  1. import service from '@/utils/request'
  2. export default {
  3. test() {
  4. var formData = new FormData(); // Currently empty
  5. formData.append('username', '王静飞');
  6. return service({ // Promise
  7. url:'http://localhost:8080/dev-api/getMockPersonInfo',
  8. method: 'post',
  9. data: formData
  10. })
  11. }
  12. }

image.png

3、Content-Type: application/x-www-form-urlencoded

需要安装qs 模块
npm install qs —save

  1. import service from '@/utils/request'
  2. import qs from 'qs'
  3. export default {
  4. test() {
  5. var qs_data = qs.stringify({
  6. "name": "王静飞",
  7. "pagesize":"20",
  8. "curpage":"1"
  9. })
  10. return service({ // Promise
  11. url:'http://localhost:8080/dev-api/getMockPersonInfo',
  12. method: 'post',
  13. data: qs_data
  14. })
  15. }
  16. }

postman 测试: key和value都不需要加双引号
image.png

image.png

参考连接: https://segmentfault.com/a/1190000015261229