diff --git a/vueblog/src/utils/api.js b/vueblog/src/utils/api.js new file mode 100644 index 0000000..8bb6164 --- /dev/null +++ b/vueblog/src/utils/api.js @@ -0,0 +1,71 @@ +import axios from 'axios' + +let base = ''; +export const postRequest = (url, params) => { + return axios({ + method: 'post', + url: `${base}${url}`, + data: params, + transformRequest: [function (data) { + // Do whatever you want to transform the data + let ret = '' + for (let it in data) { + ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&' + } + return ret + }], + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + }); +} +export const uploadFileRequest = (url, params) => { + return axios({ + method: 'post', + url: `${base}${url}`, + data: params, + headers: { + 'Content-Type': 'multipart/form-data' + } + }); +} +export const putRequest = (url, params) => { + return axios({ + method: 'put', + url: `${base}${url}`, + data: params, + transformRequest: [function (data) { + let ret = '' + for (let it in data) { + ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&' + } + return ret + }], + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + }); +} +export const deleteRequest = (url) => { + return axios({ + method: 'delete', + url: `${base}${url}` + }); +} +export const getRequest = (url,params) => { + return axios({ + method: 'get', + data:params, + transformRequest: [function (data) { + let ret = '' + for (let it in data) { + ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&' + } + return ret + }], + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + }, + url: `${base}${url}` + }); +}