vue-axios设置公共的请求ip
1.安装axios,网上找方法
2.src->network->request.js并复制:
import axios from 'axios'
export function request(config) {
const instance = axios.create({
baseURL: "http://xxx.xxx.xxx.xxx:8085/api",
})
instance.interceptors.request.use(config => {
if(config.method === "post"){
config.data = JSON.stringify(config.data)
}
return config
},error => {
console.log(error)
})
instance.interceptors.response.use(res =>{
return res.data
},error => {
console.log(error)
})
return instance(config)
}
3.使用:src->network->register.js并复制:
import {request} from "./request";
export function register(mobile,password) {
return request({
url:'api/register',
params:{
mobile,
password
},
method:'Post',
headers: { 'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded'},
})
}
=====================
键值对参数:
export function logout(){
var user = JSON.parse(localStorage.getItem('user'))
return request({
url:'/api/logout',
data:{
"userId":user.user.userId,
},
method:'Post',
headers:{
'post':{'Content-Type': 'application/json;charset=UTF-8'},
'token':user.token
}
})
}
在使用页面:
import {register} from '../network/register'
registerBtn(){
register(this.mobile,this.password).then(res=>{
console.log(res)
})
}