码迷,mamicode.com
首页 > 移动开发 > 详细

axios使用方法

时间:2018-11-21 10:58:34      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:ret   ati   highlight   ade   调用   ==   ase   使用方法   baseurl   

npm install axios

创建文件夹api/index.js

import axios from ‘axios‘;

let http = axios.create({
  baseURL: ‘‘,
  changeOrigin: true, 
  withCredentials: true,
  headers: {
    ‘Content-Type‘: ‘application/x-www-form-urlencoded;charset=utf-8‘
  },
  transformRequest: [function (data) {
    let newData = ‘‘;
    for (let k in data) {
      if (data.hasOwnProperty(k) === true) {
        newData += encodeURIComponent(k) + ‘=‘ + encodeURIComponent(data[k]) + ‘&‘;
      }
    }
    return newData;
  }]
});

function apiAxios(method, url, params, response) {
  http({
    method: method,
    url: url,
    data: method === ‘POST‘ || method === ‘PUT‘ ? params : null,
    params: method === ‘GET‘ || method === ‘DELETE‘ ? params : null,
  }).then(function (res) {
    response(res);
  }).catch(function (err) {
    response(err);
  })
}

export default {
  get: function (url, params, response) {
    return apiAxios(‘GET‘, url, params, response)
  },
  post: function (url, params, response) {
    return apiAxios(‘POST‘, url, params, response)
  },
  put: function (url, params, response) {
    return apiAxios(‘PUT‘, url, params, response)
  },
  delete: function (url, params, response) {
    return apiAxios(‘DELETE‘, url, params, response)
  }
}

  

main.js

import Api from ‘./api/index.js‘;
Vue.prototype.$api = Api;

  


调用
 this.$api.get(‘/apis/index.php?act=login‘, {
                    "act": "login"
                  }, response => {
                    this.msg=response.data+‘1‘;
                  },error => {
                    this.msg=‘error‘;
                  }
                
                );

  



axios使用方法

标签:ret   ati   highlight   ade   调用   ==   ase   使用方法   baseurl   

原文地址:https://www.cnblogs.com/vxianfeng/p/9993532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!