vue-resource的使用
直接在页面中,通过script标签,引入vue-resource的脚本文件;
注意:引用的先后顺序是 - 先引用Vue的脚本文件,再引用vue-resource的脚本文件
get请求
getInfo() { // get 方式获取数据
this.$http.get('http://yapi.shangyuninfo.com/mock/36/web02/category').then(res => {
console.log(res.body);
})
}
post请求
postInfo() { var url = ' http://yapi.shangyuninfo.com/mock/36/web02/list/goods/category'; // post 方法接收三个参数: // 参数1: 要请求的URL地址 // 参数2: 要发送的数据对象 // 参数3: 指定post提交的编码类型为 application/x-www-form-urlencoded this.$http.post(url, { categoryId: 'zs' }, { emulateJSON: true }).then(res => { console.log(res.body); }); }
注意:
编码类型post指定为 application/x-www-form-urlencoded 时要写
{ emulateJSON: true }
不是form格式时不需要加

浙公网安备 33010602011771号