vue简学之路(案例二)axios请求get及post以及全局拦截器
2020-01-15 10:13
344 查看
axios请求
在这里要先安装axios,我的例子是直接引入网上的
命令如下 npm install axios 可使用的idea直接安装
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>axios请求</title> <script src="vue.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <!--使用axios請求獲得文件內容--> <div id="app"> <h1> {{msg}} </h1> <button @click="getmsg()">click</button> <button @click="postmsg()">postclick</button> </div> <script> var vm = new Vue({ el:"#app", data:{ msg:"" }, //生命周期函数,组件在被挂载的时候调用 mounted(){ //全局拦截器,可以对请求修改 axios.interceptors.request.use(function(config){ console.log("请求马上就要发出了,在拦截请求之前发送") return config }) axios.interceptors.response.use(function (response) { console.log(response) response.data="修改为董国庆" console.log("拦截到响应") return response }) }, methods:{ //請求 getmsg:function() { // 需要npm安装axios axios.get("./test.txt",{params:{id:1},headers:{token:"123"}}).then(res=>{ this.msg=res.data console.log("ok") }).catch(res=>{ console.log(err) }) }, postmsg:function() { axios.post("./test.txt",{name:"admin",age:18}).then(res=>{ console.log("ok") }).catch(res=>{ console.log(err) }) } } }) </script> </body> </html>
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- iOS开发 - NSURLSession简单介绍及POST、GET请求案例
- iOS中线程以及GET和POST请求的一些知识
- iOS开发:GET与POST接口网络请求以及对AFNetworking的二次封装
- vue开发:axios的post请求接口出现404,但是get则成功
- IOS开发笔记-NSURLConnection同步请求和异步请求以及get方法,post方法
- iOS中线程以及GET和POST请求的一些知识
- vue中axios处理http发送请求的示例(Post和get)
- vue中axios数据请求 get和post
- vue简学之路(案例三)axios 管道符以及生命周期
- IOS之同步请求、异步请求、GET请求、POST请求
- [ios]get,post请求 【转】
- ios网络请求 get——post 区别
- Nohttp网络请求数据,Post以及Get的简单实用以及设置缓存文字的的请求
- C#如何进行https的GET与POST请求以及照片上传
- iOS开发网络篇—发送GET和POST请求(使用NSURLSession)
- ajax请求post和get的区别以及get post的选择
- 【Angular】angular-HttpClient 与服务器通讯 Get Post Put Delete Http 拦截器 请求讲解
- 【Angular】angular-HttpClient 与服务器通讯 Get Post Put Delete Http 拦截器 请求讲解
- ios-程序中模拟GET和POST请求登录
- 浅谈使用Fiddler工具发送post请求(带有json数据)以及get请求(Header方式传参)