当前位置:   article > 正文

axios配置请求头content-type 和 get/post请求方式_axios content-type

axios content-type

axios配置请求头content-type 

 https://blog.csdn.net/wojiushiwo945you/article/details/107653962

  • axios 是Ajax的一个插件,axios虽然是一个插件,但是我们不需要通过Vue.use(axios)来使用,下载完成后,只需在项目中引入即可。(一般我们放在了请求接口的公共文件中引用)
npm install axios -S
  • axios 发送post请求时默认是直接把 json 放到请求体中提交到后端的,axios默认的请求头content-type类型是’application/json;charset=utf-8’.

  • content-type的三种常见数据格式:

  1. // 1 默认的格式请求体中的数据会以json字符串的形式发送到后端
  2. 'Content-Type: application/json '
  3. // 2 请求体中的数据会以普通表单形式(键值对)发送到后端
  4. 'Content-Type: application/x-www-form-urlencoded'
  5. // 3 它会将请求体的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件
  6. 'Content-Type: multipart/form-data'
  • Content-Type: application/json这种参数是默认的就不说了
  • 若后端需要接受的数据类型为:application/x-www-form-urlencoded,我们前端该如何配置:
    1 用 URLSearchParams 传递参数
  1. var param = new URLSearchParams()
  2. param.append('name',name)
  3. param.append('age' , age)
  4. axios(
  5. {
  6. method:'post',
  7. url: url,
  8. data : param,
  9. }
  10. ).then(res => res).catch(err => err)

2 配置axios请求头中的content-type为指定类型(这个比较常用)

axios.defaults.headers["Content-Type"] = "application/x-www-form-urlencoded";

3 引入 qs ,这个库是 axios 里面包含的,不需要再下载了

  1. import Qs from 'qs'
  2. let params= {
  3. "name": "ll",
  4. "age": "18"
  5. }
  6. axios({
  7. headers: {
  8. 'Content-Type': 'application/x-www-form-urlencoded'
  9. },
  10. method: 'post',
  11. url: url,
  12. data: Qs.stringify(params)
  13. })
  • 若后端需要接受的数据类型为:Content-Type: multipart/form-data,我们前端该如何配置:
    应用场景:对于这种类型的数据,我们常见前端页面上传个人图像,然后点击保存发送后端修改原始数据
  1. let params = new FormData()
  2. params.append('file', this.file)
  3. params.append('qq', this.qq)
  4. params.append('weChat', this.WeChat)
  5. axios.post(URL, params, {headers: {'Content-Type': 'multipart/form-data'}}).then(res => {
  6. if (res.data.code === 0) {
  7. this.$router.go(-1)
  8. }
  9. }).catch(error => {
  10. alert('更新用户数据失败' + error)
  11. })

axios中get/post请求方式

1. 前言

最近突然发现post请求可以使用params方式传值,然后想总结一下其中的用法。

2.1 分类

image.png


get请求中没有data传值方式
经过查阅资料,get请求是可以通过body传输数据的,但是许多工具类并不支持此功能。
在postman中,选择get请求后,body选项自动变为了灰色。
即,不建议使用此方式传输数据。

2.2 get请求

params

基础类型接收,名字对应即可
  1. // method
  2. const params = {
  3. id: '123456789',
  4. name: '张三'
  5. }
  6. test(params)
  7. // api
  8. export function test (params) {
  9. return axios({
  10. url: url,
  11. method: 'GET',
  12. params: params
  13. })
  14. }
  15. // 后台
  16. @GetMapping("/test")
  17. public Result test(Long id, String name) {
  18. return Res.ok();
  19. }
使用Map接收,需要添加 RequestParam 注解
  1. // method
  2. const params = {
  3. id: '123456789',
  4. name: '张三'
  5. }
  6. test(params)
  7. // api
  8. export function test (params) {
  9. return axios({
  10. url: url,
  11. method: 'GET',
  12. params: params
  13. })
  14. }
  15. // 后台
  16. @GetMapping("/test")
  17. public Result test(@RequestParam Map<String, Object> map) {
  18. return Res.ok();
  19. }
使用实体类接收
  1. // 实体类
  2. @Data
  3. public class TestEntity {
  4. Long id;
  5. String name;
  6. }
  7. // method
  8. const params = {
  9. id: '123456789',
  10. name: '张三'
  11. }
  12. test(params)
  13. // api
  14. export function test (params) {
  15. return axios({
  16. url: url,
  17. method: 'GET',
  18. params: params
  19. })
  20. }
  21. // 后台
  22. @GetMapping("/test")
  23. public Result test(TestEntity testEntity) {
  24. return Res.ok();
  25. }

ps: get请求不允许传递List,需要使用qs插件或者配置axios,具体参考链接

2.3 post请求

2.3.1 params 与 get方式相同

与get相似,基础类型接收,名字对应即可
  1. // method
  2. const params = {
  3. id: '123456789',
  4. name: '张三'
  5. }
  6. test(params)
  7. // api
  8. export function test (params) {
  9. return axios({
  10. url: url,
  11. method: 'POST',
  12. params: params
  13. })
  14. }
  15. // 后台
  16. @PostMapping("/test")
  17. public Result test(Long id, String name) {
  18. return Res.ok();
  19. }
与get相似,使用map接收
  1. // method
  2. const params = {
  3. id: '123456789',
  4. name: '张三'
  5. }
  6. test(params)
  7. // api
  8. export function test (params) {
  9. return axios({
  10. url: url,
  11. method: 'POST',
  12. params: params
  13. })
  14. }
  15. // 后台
  16. @PostMapping("/test")
  17. public Result test(@RequestParam Map<String, Object> map) {
  18. return Res.ok();
  19. }
与get相似,使用实体类接收
  1. // 实体类
  2. @Data
  3. public class TestEntity {
  4. Long id;
  5. String name;
  6. }
  7. // method
  8. const params = {
  9. id: '123456789',
  10. name: '张三'
  11. }
  12. test(params)
  13. // api
  14. export function test (params) {
  15. return axios({
  16. url: url,
  17. method: 'POST',
  18. params: params
  19. })
  20. }
  21. // 后台
  22. @PostMapping("/test")
  23. public Result test(TestEntity testEntity) {
  24. return Res.ok();
  25. }

2.3.2 data

使用实体类接收
  1. // 实体类
  2. @Data
  3. public class TestEntity {
  4. Long id;
  5. String name;
  6. }
  7. // method
  8. const params = {
  9. id: '123456789',
  10. name: '张三'
  11. }
  12. test(params)
  13. // api
  14. export function test (params) {
  15. return axios({
  16. url: url,
  17. method: 'POST',
  18. data: params
  19. })
  20. }
  21. @PostMapping("/test")
  22. public Result test(@RequestBody TestEntity testEntity) {
  23. return Res.ok();
  24. }

JAVA 复制 全屏

4. 总结

总体来说,只要使用 params get与post请求基本是一样使用的,如果参数名与传递名称不一致,需要使用@RequestParam修饰,若使用Map接收参数,必须使用@RequestParam修饰。但是如果想传list类型的数据,需要使用单独的方法处理(参考链接)。
若使用data传递参数,必须使用一个实体类接收参数,而且需要添加注解@RequestBody进行修饰。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/715451
推荐阅读
相关标签
  

闽ICP备14008679号