背景:請求失敗後,因跨域引發的不能傳遞statusCode問題,經過設置先後臺選項解決,這裏先總結一下axios的使用vue
1、安裝與配置:node
安裝:ios
npm install axios
axios使用API:es6
1.默認使用的是get請求:vue-cli
// Send a GET request (default method) axios('/user/12345');
2.經常使用2種請求方式:npm
// GET :獲取圖片 axios({ method:'get', url:'http://bit.ly/2mTM3nY', responseType:'stream' }) .then(function (response) { response.data.pipe(fs.createWriteStream('ada_lovelace.jpg')) }); // Send a POST request axios({ method: 'post', url: '/user/12345', data: { firstName: 'Fred', lastName: 'Flintstone' } });
3.自定義配置axios:json
// 全局more設置 axios.defaults.baseURL = 'https://api.example.com'; axios.defaults.headers.common['Authorization'] = AUTH_TOKEN; axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //自定義設置 const instance = axios.create({ baseURL: 'https://some-domain.com/api/', timeout: 1000, headers: {'X-Custom-Header': 'foobar'} });
// 全局與個別請求設置 instance.defaults.timeout = 2500; instance.get('/longRequest', { timeout: 5000 });
// 設置token const CancelToken = axios.CancelToken; const source = CancelToken.source(); axios.get('/user/12345', { cancelToken: source.token }).catch(function (thrown) { if (axios.isCancel(thrown)) { console.log('Request canceled', thrown.message); } else { // handle error } }); axios.post('/user/12345', { name: 'new name' }, { cancelToken: source.token }) // cancel the request (the message parameter is optional) source.cancel('Operation canceled by the user.');
// 自定義取消token const CancelToken = axios.CancelToken; let cancel; axios.get('/user/12345', { cancelToken: new CancelToken(function executor(c) { // An executor function receives a cancel function as a parameter cancel = c; }) }); // cancel the request cancel();
4.請求配置:axios
{ url: '/user', method: 'get', // default baseURL: 'https://some-domain.com/api/', transformRequest: [function (data, headers) { return data; }], transformResponse: [function (data) { return data; }], headers: {'X-Requested-With': 'XMLHttpRequest'}, params: { ID: 12345 }, paramsSerializer: function (params) { return Qs.stringify(params, {arrayFormat: 'brackets'}) }, data: { firstName: 'Fred' }, // 設置請求超時. timeout: 1000, // default is `0` (no timeout) // 【是否使用憑證】 withCredentials: false, // default adapter: function (config) { /* ... */ }, auth: { username: 'janedoe', password: 's00pers3cret' }, responseType: 'json', // default responseEncoding: 'utf8', // default xsrfCookieName: 'XSRF-TOKEN', // default xsrfHeaderName: 'X-XSRF-TOKEN', // default onUploadProgress: function (progressEvent) { // Do whatever you want with the native progress event }, onDownloadProgress: function (progressEvent) { // Do whatever you want with the native progress event }, maxContentLength: 2000, validateStatus: function (status) { return status >= 200 && status < 300; // default }, maxRedirects: 5, // default socketPath: null, // default httpAgent: new http.Agent({ keepAlive: true }), httpsAgent: new https.Agent({ keepAlive: true }), proxy: { host: '127.0.0.1', port: 9000, auth: { username: 'mikeymike', password: 'rapunz3l' } }, cancelToken: new CancelToken(function (cancel) { }) }
5.返回配置:api
{ // `data` is the response that was provided by the server data: {}, // `status` is the HTTP status code from the server response status: 200, // `statusText` is the HTTP status message from the server response statusText: 'OK', // `headers` the headers that the server responded with // All header names are lower cased headers: {}, // `config` is the config that was provided to `axios` for the request config: {}, // `request` is the request that generated this response request: {} }
6.攔截操做:返回正確後操做,返回錯誤操做跨域
axios.get('/user/12345') .then(function (response) { console.log(response.data); console.log(response.status); console.log(response.statusText); console.log(response.headers); console.log(response.config); });
axios.get('/user/12345') .catch(function (error) { if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx console.log(error.response.data); console.log(error.response.status); console.log(error.response.headers); } else if (error.request) { // The request was made but no response was received // `error.request` is an instance of XMLHttpRequest in the browser and an instance of // http.ClientRequest in node.js console.log(error.request); } else { // Something happened in setting up the request that triggered an Error console.log('Error', error.message); } console.log(error.config); });
在main.js導入:
// 引入axios,並加到原型鏈中 import axios from 'axios'; Vue.prototype.$axios = axios; import QS from 'qs' Vue.prototype.qs = QS;
設置代理解決跨域:
import axios from 'axios' import qs from 'qs' axios.defaults.timeout = 5000; //響應時間 axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'; //配置請求頭 axios.defaults.baseURL = ''; //配置接口地址 //POST傳參序列化(添加請求攔截器) axios.interceptors.request.use((config) => { //在發送請求以前作某件事 if(config.method === 'post'){ config.data = qs.stringify(config.data); } return config; },(error) =>{ console.log('錯誤的傳參') return Promise.reject(error); }); //返回狀態判斷(添加響應攔截器) axios.interceptors.response.use((res) =>{ //對響應數據作些事 if(!res.data.success){ return Promise.resolve(res); } return res; }, (error) => { console.log('網絡異常') return Promise.reject(error); }); //返回一個Promise(發送post請求) export function fetchPost(url, params) { return new Promise((resolve, reject) => { axios.post(url, params) .then(response => { resolve(response); }, err => { reject(err); }) .catch((error) => { reject(error) }) }) } ////返回一個Promise(發送get請求) export function fetchGet(url, param) { return new Promise((resolve, reject) => { axios.get(url, {params: param}) .then(response => { resolve(response) }, err => { reject(err) }) .catch((error) => { reject(error) }) }) } export default { fetchPost, fetchGet, }
vue-cli 3.0的在 package.json 同級目錄新建一個 vue.config.js 文件,加入下面代碼,其餘版本找到配置文件的devServer加入代碼,以下:
module.exports = { //axios域代理,解決axios跨域問題 baseUrl: '/', devServer: { proxy: { '': { target: 'http://192.168.0.108:8090', changeOrigin: true, ws: true, pathRewrite: { } } } } }
實例:get/post
const axios = require('axios'); // 1.在url中攜帶 axios.get('/user?ID=12345') // get .then(function (response) { // handle success console.log(response); }) .catch(function (error) { // handle error console.log(error); }) .then(function () { // always executed }); // 2.經過對象params傳遞 axios.get('/user', { params: { ID: 12345 } }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }) .then(function () { // always executed }); // 3.異步請求 async function getUser() { try { const response = await axios.get('/user?ID=12345'); console.log(response); } catch (error) { console.error(error); } }
axios.post('/user', { // post firstName: 'Fred', lastName: 'Flintstone' }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
執行多個併發請求:
function getUserAccount() { return axios.get('/user/12345'); } function getUserPermissions() { return axios.get('/user/12345/permissions'); } axios.all([getUserAccount(), getUserPermissions()]) .then(axios.spread(function (acct, perms) { // Both requests are now complete }));
使用封裝請求https.js,簡化操做:
import https from '../https.js' // 注意用本身的路徑 loginPost: function () { let params ={'username': 'admin', 'password': 'admin123'} https.fetchPost('/login',params ).then((data) => { // console.log('fetchPost',data) }).catch(err=>{ console.log(err) } ) }, indexPost2:function (date) { // ... },
補:默認狀況下,axios將JavaScript對象序列化爲JSON
。要以application/x-www-form-urlencoded
格式
1.Json對象轉字符串:用JSON.stringify()將對象a變成了字符串c,那麼我就能夠用JSON.parse()將字符串c還原成對象a
let arr = [1,2,3]; JSON.stringify(arr);//'[1,2,3]' typeof JSON.stringify(arr);//string let string = '[1,2,3]'; console.log(JSON.parse(string))//[1,2,3] console.log(typeof JSON.parse(string))//object
做用1:判斷是否包含、相等、
//判斷數組是否包含某對象 let data = [ {name:'echo'}, {name:'聽風是風'}, {name:'天子笑'}, ], val = {name:'天子笑'}; JSON.stringify(data).indexOf(JSON.stringify(val)) !== -1;//true //判斷兩數組/對象是否相等 let a = [1,2,3], b = [1,2,3]; JSON.stringify(a) === JSON.stringify(b);//true
做用2:本地數據緩存
//存 function setLocalStorage(key,val){ window.localStorage.setItem(key,JSON.stringify(val)); }; //取 function getLocalStorage(key){ let val = JSON.parse(window.localStorage.getItem(key)); return val; }; //測試 setLocalStorage('demo',[1,2,3]); let a = getLocalStorage('demo');//[1,2,3]
做用3:深拷貝
//深拷貝 function deepClone(data) { let _data = JSON.stringify(data), dataClone = JSON.parse(_data); return dataClone; }; //測試 let arr = [1,2,3], _arr = deepClone(arr); arr[0] = 2; console.log(arr,_arr)//[2,2,3] [1,2,3]
區分:toString()雖然能夠將數組轉爲字符串,但並不能對{name:'天子笑'}這類對象實現你想要的操做,它的受衆更可能是數字。
2.node,js:
const querystring = require('querystring'); // node.js內 axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));
3.使用「qs」工具轉換:
// const qs = require('qs'); import qs from 'qs'; // 推薦使用es6 axios.post('/foo', qs.stringify({ 'bar': 123 })); // 方式1 const data = { 'bar': 123 }; const options = { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, data: qs.stringify(data), url, }; axios(options); // 方式2