Vue的axios插件实现GET、POST、JSONP请求,以及请求拦截
2019-01-07 13:19
597 查看
1.需要引入vue.js和axios.js(小编比较懒直接引用的bootstrap来做样式)
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="../node_modules/vue/dist/vue.js"></script> <script src="../node_modules/axios/dist/axios.js"></script>
2.在HTML中请求按钮
<div id="app" class="container"> <h1>axios</h1> <a href="javascript:;" @click="get" class="btn btn-primary">GET请求</a> <a href="javascript:;" @click="post" class="btn btn-primary">POST请求</a> <a href="javascript:;" @click="http" class="btn btn-primary">http请求</a> <a href="javascript:;" @click="jsonp" class="btn btn-primary">jsonp请求</a><br><br> <span> <span>axios的http请求中method的传参:</span> <br><br> <span class="btn btn-success">post请求是通过data传递参数</span> <span class="btn btn-success">get请求是通过params传递参数</span> </span> <br> <span>{{msg}}</span> </div>
3.这里就是重点啦!请求+拦截
<script> new Vue({ el: '#app', data: { msg: '' }, mounted: function () { axios.interceptors.request.use(function (config) { console.log('request init' + config); //可以添加请求前的 --- Loading效果处理 return config; }); axios.interceptors.response.use(function (response) { console.log('response init' + response); //可以添加响应过后的 --- 效果处理 return response; }); }, methods: { get: function () { axios.get('../package.json', { params: { userId: '105' }, headers: { token: '123' }, before: function () { console.log('before init'); } }).then(res => { this.msg = res.data; }).catch(function (error) { console.log('error init' + error); }) }, post: function () { axios.post('../package.json', { userId: '333', headers: { token: 'Mr.wang' } }).then(res => { this.msg = res.data; }) }, http: function () { axios({ url: '../package.json', method: 'get', data: { userId: '106' }, headers: { token: 'http-test' } }).then(res => { this.msg = res.data; console.log(res.data); }) }, jsonp: function () { axios({ headers: { 'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json; charset=UTF-8', 'Access-Control-Allow-Origin': '*' },//设置跨域请求头 method: "POST",//请求方式 url: "https://solelynet.com/public/index.php/api/v1/UserMenu/GetTree",//请求地址 data: { "menu_id": 1, "thirdapp_id": 1//请求参数 } }) .then(function (res) { //返回值 console.log(res.data); for (var i = 0; i < res.data.length; i++) { console.log(res.data[i].name) } }) .catch(function (err) { console.log(err); }); } } }) </script>
相关文章推荐
- Vue的axios插件实现GET、POST、JSONP请求,以及请求拦截
- Vue------第五天(Vue的Ajax请求,使用Axios,目前熟悉了一下,主要包括GET请求、POST请求、并发请求、请求参数的配置、服务器响应的结构以及对服务器响应错误的简单处理)
- Vue中vue-resource实现get,post,jsonp请求
- vue-resource实现get、post、jsonp请求【Vue.js】
- vue-resource 实现 get, post, jsonp请求 --Y.
- vue-resource实现get,post,jsonp请求
- vue+axios+element-ui 实现ajax请求拦截和路由拦截
- Ajax用法详细解析, 实现异步更新页面, 以及'post'和'get'请求属性介绍
- vue-resource 中 get / post / jsonp 三种请求方式的异同
- vue-resource、Vue中response的get、post和jsonp请求例子
- HttpClient 4.5版本实现文件上传下载以及常用post、get请求
- vue引入axios发送get或post请求的方法
- 不需要用vuex,只需要用到axios一样可以实现登录拦截,想后台请求数据
- 使用PHP的curl扩展实现跨域post请求,以及file_get_contents()百度短网址例子
- vue-cli3.x中使用axios发送请求,配合webpack中的devServer编写本地mock数据接口(get/post/put/delete)...
- vue-resource处理get,post,jsonp请求
- vue-cli的axios使用方法,get、post请求的跨域问题解决
- 在Vue中使用axios请求拦截的实现方法
- FlyJSONP——实现跨域Get & Post请求
- vue基础之使用get、post、jsonp实现交互功能示例