一、安装axios

yarn add axios

  1. import axios from 'axios'; // 是在.vue文件中 写了这个就不用写下一行 不用挂载到原型上
  2. Vue.prototype.axios = axios
  1. <script>
  2. import axios from 'axios';
  3. export default {
  4. name: 'home',
  5. data(){
  6. return{
  7. list:[]
  8. }
  9. },
  10. mounted(){
  11. axios.get("http://yapi.demo.qunar.com/mock/36046/cart").then(res=>{
  12. this.list = res.data;
  13. console.log(this.list)
  14. })
  15. }
  16. }
  17. </script>

二、vue中axios发送请求(请求成功弹框,并把input输入框的值清空)

接口地址:https://devqixin.91djb.net/score-mall-v19/service/taylor/swift.php
数据结构:{
“func”: “t:lotteryBaby”,
“params”: {
“enterprise”: “保护伞公司”,
“product”: “Tesla”,
“phone”: “13800138000”,
“city”: “浣熊市”,
“name”: “威斯克”
}
}
直接上代码

  1. handleClick() {
  2. var that =this;
  3. console.log("发送数据");
  4. axios({
  5. url:
  6. "https://devqixin.91djb.net/score-mall-v19/service/taylor/swift.php",
  7. method: "post",
  8. data: {
  9. func: "t:lotteryBaby",
  10. params: {
  11. enterprise: that.enterprise,
  12. product: that.product,
  13. phone: that.phone,
  14. city: that.city,
  15. name: that.name,
  16. },
  17. },
  18. headers: {
  19. "Content-Type": "application/json",
  20. },
  21. })
  22. .then(function(response) {
  23. console.log(response);
  24. if(response.data.code == 200){
  25. alert("提交成功")
  26. that.enterprise ='';
  27. that.product ='';
  28. that.phone='';
  29. that.city='';
  30. that.name='';
  31. }
  32. })
  33. .catch(function(error) {
  34. console.log(error);
  35. });
  36. }

返回数据
image.png
成功!