1. Perform common cross-domain configuration code under index.js of config;
proxyTable: {
'/apis': { //Use "/api" instead of "http://xxxx.cn"
target: 'http:// xxxx.cn',//source address (interface domain name)
changeOrigin:true,//change origin (whether it is cross domain)
pathRewrite: {
'^/apis': 'http://xxxx.cn'//path rewrite( Shorthand for normal request interface)
}
}
}
2. Use the post method of axios to initiate a request
this.$axios.post("/apis/test/testToken.php",{username:"hello",password:"123456"})
.then(res => {
console.log(res)
}).catch(err=>{
console.log(err)
})
3. These common settings of axios can be written in the api that encapsulates axios or in
main.js axios.defaults.headers.common['token'] = "f4c902c9ae5a2a9d8f84868ad064e706" // Set the token in the header according to the requirements Set axios.defaults.headers.post["Content-type"] = "application/json" // Set the request header or not Vue.prototype.$http = axios // You can use this.$http globally to initiate ask
4. Use the form of fetch API to request interface data
fetch("/apis/test/testToken.php" , { method: "post", headers: { "Content-type": "application/json", token: "f4c902c9ae5a2a9d8f84868ad064e706" }, body: JSON.stringify({ username: "henry", password: "321321" }) }) .then(result => { console.log(result) return result.json() }) .then(data => { console.log(data) })