vue axios 应用

vue安装axios

cnpm install axios
安装成功后/项目/node_modules/目录下有axios文件夹
在package.json文件中devDependencies字段中添加插件名和插件版本
   "axios": "^0.17.1",
   
在vue项目中引用axios
1.src/main.js
import axios from 'axios'
Vue.prototype.$axios = axios


 show: function(){
  //get方式

 //赋值给变量self
 var self = this;

 var url = "hotcity.json";
  axios.get(url,{
      params:{
      username: "egon"
   }
  })
.then(function (response) {
    self.stu = response.data.data.hotCity;
    console.log(response.data.data.hotCity);
})
.catch(function (error) {
   console.log(error);
})

}


show: function(){
       //post方式
 //var url = "http://bugwhy.com/php/index/getNews.php";
 var url = "http://localhost:8000/login";
       axios.post(url,{
           name: this.username,
  password: this.password
 },{
           "headers": {"Content-Type": "application/x-www-form-urlencodeed"}
 }).then(function(res){
       console.log(res.data);
  })
  .catch(function (error) {
      console.log(error);
                 })

}

  

原文地址:https://www.cnblogs.com/morgana/p/7863082.html