项目接口返回
code: 500
data: null
message: “Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported”
原因在于,接口不支持application/x-www-form-urlencoded;charset=UTF-8
通过看swagger的接口传递数据类型来修改,
将axios的请求头配置为
headers: {'X-Requested-With': 'XMLHttpRequest','Content-Type': 'application/json; charset=UTF-8'}
request.js
import axios from 'axios'
import qs from 'qs'
import router from '../../router'
axios.defaults.timeout = 65000;
window.axiosCancel = [] ; // 全局定义一个存放取消请求的标识
axios.interceptors.request.use(config => {config.cancelToken = new axios.CancelToken(cancel => {window.axiosCancel.push({cancel})})// loadingreturn config// 添加取消标记}, error => {return Promise.reject(error)
})
axios.interceptors.response.use(response => {return response
},
error => {return Promise.resolve(error.response)
})
export function checkStatus(response) {// loading// 如果http状态码正常,则直接返回数据//console.log(response)if (response && (response.status === 200 || response.status === 304 || response.status === 400)) {return response.data// 如果不需要除了data之外的数据,可以直接 return response.data}// 异常状态下,把错误信息返回去return {status: -404,message: '网络请求失败,请稍后重试!'}
}
export function checkCode(res) {//console.log(res)// 如果code异常(这里已经包括网络错误,服务器错误,后端抛出的错误),可以弹出一个错误提示,告诉用户// if (res.status == -404) {// //alert(res.msg)// Message.error({// message: res.return_message,// center: true// });// }if (res.code && res.code != '0') {if (res.code == '401') {router.push({path: '/login'})}}return res
}
export function MyPostToken(url, data, token) {// data.timestap = Date.now()return axios({method: 'post',//baseURL: 'https://cnodejs.org/api/v1',url,data: qs.stringify(data),timeout: 65000,headers: {'X-Requested-With': 'XMLHttpRequest','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8','token':token ? token:''}}).then((response) => {return checkStatus(response)}).then((res) => {return checkCode(res)})
}
export function MyPostUploadPic(url, data, upload_token) {// data.timestap = Date.now()return axios({method: 'post',url,data: data,timeout: 65000,headers: {Accept: '*/*',Authorization: 'UpToken' + ' ' + upload_token,'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}}).then((response) => {return checkStatus(response)}).then((res) => {return checkCode(res)})
}
export function MyPostQs(url, data) {data.timestap = Date.now()return axios({method: 'post',//baseURL: 'https://cnodejs.org/api/v1',url,data: qs.stringify(data),timeout: 65000,headers: {'X-Requested-With': 'XMLHttpRequest','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}}).then((response) => {return checkStatus(response)}).then((res) => {return checkCode(res)})
}
export function MyPost(url, data) {data.timestap = Date.now()return axios({method: 'post',//baseURL: 'https://cnodejs.org/api/v1',url,data: data,timeout: 65000,headers: {'X-Requested-With': 'XMLHttpRequest','Content-Type': 'application/json;charset=UTF-8'}}).then((response) => {return checkStatus(response)}).then((res) => {return checkCode(res)})
}
export function MyGet(url, params) {params.timestap = Date.now()return axios({method: 'get',//baseURL: 'https://cnodejs.org/api/v1',url,params, // get 请求时带的参数timeout: 60000,headers: {'X-Requested-With': 'XMLHttpRequest'}}).then((response) => {return checkStatus(response)}).then((res) => {return checkCode(res)})
}