axios本身并不支持發(fā)送跨域的請(qǐng)求冈绊,使用vue-resource發(fā)送跨域請(qǐng)求。
vue-resource基本使用方法:
this.$http.get(url,[options])
this.$http.head(url,[options])
this.$http.delete(url,[options])
this.$http.jsonp(url,[options])? ? ??
?“跨域請(qǐng)求示例”
this.$http.jsonp(`https://api.github.com/users/${this.id}`,
{params:{listName:"data"},?jsonp:'cb',})
.then((res)=>{})
.catch((err)=>{});
this.$http.post(url,[body],[options])
this.$http.put(url,[body],[options])
this.$http.patch(url,[body],[options])?
axios基本使用方法:
import axios from 'axios';
axios({
? ? method: 'get',
? ? url: 'http://www.baidu.com?name=tom&age=23'
}).then((res)=>{
? ? console.log(res)
})
axios.get('server.php',{
? ? params: {
? ? ? ? name: 'alice',
? ? ? ? age: 19
? ? }
}).then((res)=>{})?
全局定義axios參數(shù)
axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.timeout = 2500;
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';