axios本身并不支持发送跨域的请求,使用vue-resource发送跨域请求。
vue-resource基本使用方法:
this.$http.get(url,[options])
this.$http.head(url,[options])
this.$http.delete(url,[options])
this.$http.jsonp(url,[options])
“跨域请求示例”
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参数
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';