axios 详细使用方法 及跨域请求的处理
来源:互联网 发布:python static method 编辑:程序博客网 时间:2024/06/05 03:42
安装
使用 npm:
$ npm install axios
或者 使用 bower:
$ bower install axios
或者直接使用 cdn:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
main.js设置如下
引入axios
import axios from 'axios'
挂载到vue的原型
Vue.prototype.$http = axios
在webpack.config.js(config—>index.js)文件里设置代理
dev: { env: require('./dev.env'), port: 8080, //设置访问的端口号 autoOpenBrowser: true, assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: { '/api': { target: 'http://10.10:8063', //设置调用接口域名和端口号别忘了加http changeOrigin: true, pathRewrite: { '^/api': '/' //这里理解成用‘/api’代替target里面的地址,组件中我们调接口时直接用api代替 // 比如我要调用'http://0.0:300/user/add',直接写‘/api/user/add’即可 } } }
执行 GET 请求
// 为给定 ID 的 user 创建请求axios.get('/user?ID=12345') .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });// 可选地,上面的请求可以这样做axios.get('/user', { params: { ID: 12345 } }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
执行 POST 请求
axios.post('/user', { firstName: 'Fred', lastName: 'Flintstone' }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
执行多个并发请求
function getUserAccount() { return axios.get('/user/12345');}function getUserPermissions() { return axios.get('/user/12345/permissions');}axios.all([getUserAccount(), getUserPermissions()]) .then(axios.spread(function (acct, perms) { // 两个请求现在都执行完成 }));
创建实例
可以使用自定义配置新建一个 axios 实例
axios.create([config])
var instance = axios.create({ baseURL: 'https://some-domain.com/api/', timeout: 1000, headers: {'X-Custom-Header': 'foobar'}});
参考使用说明API:https://www.kancloud.cn/yunye/axios/234845
阅读全文
0 0
- axios 详细使用方法 及跨域请求的处理
- 关于vue项目中,axios请求方式,跨域请求的处理
- axios跨域请求遇到的问题
- axios请求 跨域问题
- 解决axios跨域请求出错的问题
- Vue axios处理http请求
- Vue axios处理http请求
- axios的请求
- vue处理axios post请求传参的问题
- axios使用拦截器统一处理所有的http请求
- axios、node交互跨域请求
- vuejs中vue-resource或使用axios发送请求及解决跨域问题
- vue中axios的使用方法
- axios的跨域问题
- axios中post请求出错,由于content-type引起的跨域的解决方法
- vue开发:VUE axios发送跨域请求需要注意的问题
- vue.js使用axios实现跨域请求
- Vue用axios请求数据proxyTable解决跨域问题
- MOOC浙江大学陈月、何钦铭老师《数据结构》学习笔记01
- 机器学习博客收藏~
- 专利申请流程
- mybatis Resultmap 与 ResultType 区别
- 使用谷歌(Google)TTS服务 – Java版开源gTTS及Python gTTS
- axios 详细使用方法 及跨域请求的处理
- 计蒜客 Query on a string 线段树
- 【干货】wordpress函数实现屏蔽s.w.org提升网站速度
- 如何让多行文字与图片垂直居中
- c++中带有const ,&(引用)的常见类型的错误转换
- eclipse项目转android studio问题解决记录
- solr6.0学习之环境搭建-windows
- 珍珠BEAD -- 图
- Matlab函数padarray小结