Vue-cli中axios传参的方式以及后端取的方式

0917自我总结

Vue-cli中axios传参的方式以及后端取的方式

一.传参

  • params是添加到url的请求字符串中的,用于get请求。
  • data是添加到请求体(body)中的, 用于post请求。

首先现在main.js进行配置

import axios from 'axios'
Vue.prototype.$axios = axios;

如:get请求

<script>
    ......
    事件的函数() {
     this.$axios({
                    url: xxxxx
                    method: 'get',
                    params: {
                        变量名: 变量值
                    }
                }).then(response => {请求成功逻辑代码})..catch(error => {请求失败逻辑代码})
    }
    ........
</script>

如:post请求

<script>
    ......
    事件的函数() {
     this.$axios({
                    url: xxxxx
                    method: 'post',
                    data: {
                        变量名: 变量值
                    }
                }).then(response => {请求成功逻辑代码})..catch(error => {请求失败逻辑代码})
    }
    ........
</script>

二.后台获取

如果是params传参后台取request.GET或者request.query_params

如果是data传参后台取request.data

posted @ 2019-09-17 10:59  小小咸鱼YwY  阅读(5148)  评论(0编辑  收藏  举报