vue axios前后端数据通信

1、安装依赖

npm install axios –save

2、上代码

请求的地址改为你自己的地址

<template>
  <div class="edit_container">   
 <el-button type="primary" icon="el-icon-edit" @click="release">发布</el-button>
  </div>
</template>

<script>

  import axios from 'axios'	//引入axios,这里可以全局引入,方便其他页面使用

  export default {
    
    methods: {
      // 发布
      release() {
        axios.get('http://47.98.38.126:8081/api/name')
          .then(function(response) {
            console.log(response.data);
          })
          .catch(function(error) {
            console.log(error);
          });
      }
    },
    computed: {
      editor() {
        return this.$refs.myQuillEditor.quill;
      },
    }
  }
</script>
3、GET、POST

GET

axios.get('/user?ID=12345')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

// 上面的请求可以这样做
axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

POST

注意:POST方法传data必须要用字符串拼接的方法,否则会报错!
想要写成json方法去其他博主那里看看吧,目前还在学习中!

axios.post('http://127.0.0.1:8081/api/release','date='+objdate+'&author=xieyo').then(function(res){
          console.log(res)
        });

你可能感兴趣的:(vue.js)