vue之 axios的post请求及headers token方式

所遇

GET请求 和 POST请求的例子

<script> 
import axios from 'axios'  //引入axios
export default {
   name: 'list',

   methods:{
    getProduct () {     
      axios.get("http://www.aaa.com")
        .then(function(res){
          console.log('get请求...'); 
          console.log(res.data);  
      });  

      axios.post("http://www.aaa.com",{id:3,name:'abc'},{
        headers: {
           'content-type': 'application/json',
           "token":'14a1347f412b39f'  //token换成从缓存获取
        }
      }).then(function(res){
          console.log('post请求...'); 
          console.log(res.data);  
      });     
    } 
  }, 

  //vue生命周期函数
  mounted (){ 
    this.getProduct();
  }   
}
</script>

headers要放在params的后面,不能和params在一个参数中。加不加params都可以,看api规范

网上有些传递headers的教程如下,我api用的thinkphp,这样提交传递是在body中,而不是headers中

 axios.post("http://www.aaa.com",{
    params:{
      id:3,name:'abc'
    },
    headers:{
      'token':'123123213123'
   }
}

微信公号搜索:李大头头。(或扫描二维码)找我来玩。

更多【前端技术群】和【内推职位】 资源等着你。

还不定期在公众号请大家喝奶茶!

想与我聊聊或者有什么问题都可以在公众号找到我。

等你哟~

猜你喜欢

转载自blog.csdn.net/ljy_1024/article/details/97006783