axios發送ajax請求

安裝:

npm install axios --save

package.json

如圖所示,安裝成功。javascript

在main.js中配置

axios發送請求

mounted:function () {
      //vue頁面加載時自動執行
      this.initCourse()
    },
    methods:{
      initCourse:function () {
        var that=this
        //經過ajax向接口發送請求,並獲取課程列表
        //axios發送ajax請求
        //npm install axios --save
        //第一步:在main.js中配置
        //第二步:使用axios發送請求
        this.$axios.request({
          url:'http://127.0.0.1:8000/luffy/v1/course/',
          method:'GET'
        }).then(function (ret) {
          //ajax請求發送成功後,獲取的相應內容
            if(ret.data.code===1000){
              that.CourseList=ret.data.data
            }else{
              alert("獲取數據失敗")
            }
        }).catch(function (ret) {
          //ajax請求失敗以後,獲取相應的內容

        })
      }
相關文章
相關標籤/搜索