axios 设置headers头部传参token

引入了js-cookie
// axios.defaults.headers.get['Content-Type'] = 'application/json;charset=UTF-8'
export function httpget(url,params){
  console.log(ip + url)
  return new Promise((resolve,reject) => {
    axios.get(ip + url,{
    params:params
  }).then( (response) => {
      if(response.data.code == 1003) {
     _IsIOS()
      } else {
         var Token = response.headers.token
        if(!Token) {
          return resolve(response.data)
        } else {
          var token = this.Cookies.get('token')
          var userId = token.split("_")[0]
          var Token = response.headers.token
          this.Cookies.set("token", userId + '_' + Token)
          resolve(response.data)
        }
      }

})
.catch(err => {
    reject(err)
  })
})
}





/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

//axios post请求
export function httppost(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.post(ip+url,data)
      .then(response => {
        if(response.data.code == 1003) {
          _IsIOS()
        } else {
          var Token = response.headers.token
          if(!Token) {
            return resolve(response.data)
          } else {
            var token = this.Cookies.get('token')
            var userId = token.split("_")[0]
            var Token = response.headers.token
            this.Cookies.set("token", userId + '_' + Token)
            resolve(response.data)
          }
        }
      },err => {
        reject(err)
      })
  })
}



//axios 需登录get请求
export function axiosget(url,params){
  return new Promise((resolve,reject) => {
    axios({
      method:"get",
      url:ip+url,
      data:params,
      headers:{
        "Authorization":this.Cookies.get('token')
      }
      }).then( (response) => {
      if(response.data.code == 1003) {
        _IsIOS()
      } else {
        var Token = response.headers.token
        if(!Token) {
          return resolve(response.data)
        } else {
          var token = this.Cookies.get('token')
          var userId = token.split("_")[0]
          var Token = response.headers.token
          this.Cookies.set("token", userId + '_' + Token)
          resolve(response.data)
        }
      }
    })
      .catch(err => {
        reject(err)
      })
  })
}



//axios 需登录post请求
export function axiospost(url,data={}){
  return new Promise((resolve,reject) => {
    console.log(ip+url)
    console.log(data)
    axios(
      {
        method:"post",
        url:ip+url,
        data:data,
        headers:{
          "Authorization":this.Cookies.get('token')
        }
      })
      .then(response => {
        if(response.data.code == 1003) {
          _IsIOS()
        } else {
          var Token = response.headers.token
          if(!Token) {
            return resolve(response.data)
          } else {
            var token = this.Cookies.get('token')
            var userId = token.split("_")[0]
            var Token = response.headers.token
            this.Cookies.set("token", userId + '_' + Token)
            resolve(response.data)
          }
        }
      },err => {
        reject(err)
      })
  })
}

你可能感兴趣的:(vue+apicloud)