標籤:cas 資訊 his sign 逾時 ack 封裝 push 頁面
main.js 中:
import axios from ‘................/axios‘
axios.js 中:
//axios.jsimport Vue from ‘vue‘import axios from ‘axios‘Vue.prototype.$http = axios//http request 封裝要求標頭攔截器axios.interceptors.request.use(config => { // console.log("request") // console.log(config) //請求方式 let method = config.method.toLowerCase(); if (method === ‘get‘ || method === ‘delete‘) { Object.assign(config.params, { "test": "testVAl" }); } return config;}, error => { return Promise.reject(err);});//http response 封裝後台返回攔截器axios.interceptors.response.use(response => { // console.log(response)//當返回資訊為未登入或者登入失效的時候重新導向為登入頁面 // if (response.data.code == ‘W_100004‘ || response.data.message == ‘使用者未登入或登入逾時,請登入!‘) { // router.push({ // path: "/", // querry: { // redirect: router.currentRoute.fullPath // } //從哪個頁面跳轉 // }) // } if (typeof response.data === ‘string‘) { return JSON.parse(response.data); } else { return response; }}, error => { return Promise.reject(error)});
使用:
this.$http.get(‘/api/......‘, {params:{}}).then(res => { console.log(res) }, res => { // error callback });
vue中axios 配置請求攔截功能 及請求方式如何封裝