web前端|js教程
axios,ajax,使用
web前端-js教程+ 首先,引入axios
jsp源码打包exe,vscode图书管理系统,vm下ubuntu,tomcat_jdbc配置,爬虫盗号bilibili,php 加入空格,洪梅seo优化费用,求绿色蔬菜购物网站代码包,免费新闻cms模板lzw
CDN: npm: npm install axios 并在全局的js中引入:import axios from axios;
•get请求
安卓浏览器的源码,ubuntu加载源文件,tomcat加载不成功,爬虫去标签,php抓取邮箱,渝中区中小企业seo推广效果lzw
axios.get(/user?ID=12345) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
•post请求
网页源码的第一面,h5 vscode,ubuntu-游戏,jsp放在tomcat目录,sqlite语句删除一列,初学者网络爬虫视频教程,php中的int,长春抖音seo软件,静态网站 站内搜索,个人介绍的网站模板免费下载lzw
依赖于qs包,将对象转换成以&连接的字符串//例:axios.post( postUrl ,qs.stringify({userid:1,username:yyy})).then(function (response) { console.log(response);})
附录:配置 axios
上面封装的方法中,使用了 axios 的三个配置项,实际上只有 url 是必须的,完整的 api 可以参考使用说明
为了方便,axios 还为每种方法起了别名,比如上面的 saveForm 方法等价于:
axios.post(/user, context.state.test02)
完整的请求还应当包括 .then 和 .catch
.then(function(res){ console.log(res)}).catch(function(err){ console.log(err)})
当请求成功时,会执行 .then,否则执行 .catch
这两个回调函数都有各自独立的作用域,如果直接在里面访问 this,无法访问到 Vue 实例
这时只要添加一个 .bind(this) 就能解决这个问题
.then(function(res){ console.log(this.data)}.bind(this))