axios 基本运用
axios是专门对ajax请求进行封装的一个插件,其返回一个promise对象,用法跟ES6的promise很相似
一、安装axios插件
npm install axios
二、引入axios插件
在main.js中引入,如下:
import axios from 'axios'
三、在代码中执行请求,没有配置请求方式时默认为GET请求
(1)执行GET请求
this.$http.get("https://www.ehometd.com/temporary/api/other/all.php?fc=bianlifile&FID=459&Class=3", {
params: {
ID: 459
}
})
.then(response => {
this.nowImg = response.data.Sub[460].File[0].ImgUrl; // 初始默认为第一个系列的第一张图片
this.goods_name = response.data.Sub[460].File[0].Name;
this.goods_style = response.data.Sub[460].File[0].Desc;
})
.catch(function(error) {
console.log(error);
});
}
(2)执行POST请求
this.$http.post("https://www.ehometd.com/temporary/api/other/all.php?fc=bianlifile&FID=459&Class=3", {
params: {
ID: 459
}
})
.then(response => {
this.nowImg = response.data.Sub[460].File[0].ImgUrl; // 初始默认为第一个系列的第一张图片
this.goods_name = response.data.Sub[460].File[0].Name;
this.goods_style = response.data.Sub[460].File[0].Desc;
})
.catch(function(error) {
console.log(error);
});
}
如果报错:ReferenceError: $http is not defined"
需要安装vue-resource插件,然后在main.js中引入:
import vueResource from 'vue-resource';
Vue.use(vueResource);
四、执行多个并发请求(同时执行多个请求语句)
function getUserAccount() {
return axios.get('/user/1244');
}
function getUserPromise() {
return axios.get('/user/666/promise');
}
axios.all([getUserAccount(), getUserPromise()])
.then(axios.spread(function(acct, perms){
// 两个请求都完成时才走到这里来,跟ES6的Promise.all()一样的原理
}));
axios 基本运用的更多相关文章
- 为什么axios请求接口会发起两次请求
之前在使用axios发现每次调用接口都会有两个请求,第一个请求时option请求,而且看不到请求参数,当时也没注意,只当做是做了一次预请求,判断接口是否通畅,但是最近发现并不是那么回事. 首先我们知道 ...
- axios基本用法
vue更新到2.0之后,作者就宣告不再对vue-resource更新,而是推荐的axios,前一段时间用了一下,现在说一下它的基本用法. 首先就是引入axios,如果你使用es6,只需要安装axios ...
- Axios、Lodash、TweenJS
Axios是一个基于promise的HTTP库 http://chuansong.me/n/394228451820 Lodash是一个JavaScript的函数工具集 http://www.css8 ...
- axios全攻略
随着 vuejs 作者尤雨溪发布消息,不再继续维护vue-resource,并推荐大家使用 axios 开始,axios 被越来越多的人所了解.本来想在网上找找详细攻略,突然发现,axios 的官方文 ...
- 抛弃vue-resource拥抱axios
vue-resource用法 import Vue from 'vue' import VueResource from 'vue-resource' Vue.use(VueResource) 是不是 ...
- Vue+axios 实现http拦截及路由拦截
现如今,每个前端对于Vue都不会陌生,Vue框架是如今最流行的前端框架之一,其势头直追react.最近我用vue做了一个项目,下面便是我从中取得的一点收获. 基于现在用vue+webpack搭建项目的 ...
- vue使用Axios做ajax请求
vue2.0之后,就不再对vue-resource更新,而是推荐使用axios 1. 安装 axios $ npm install axios 或 $ bower install axios 2. 在 ...
- vue全家桶(Vue+Vue-router+Vuex+axios)(Vue+webpack项目实战系列之二)
Vue有多优秀搭配全家桶做项目有多好之类的咱就不谈了,直奔主题. 一.Vue 系列一已经用vue-cli搭建了Vue项目,此处就不赘述了. 二.Vue-router Vue的路由,先献上文档(http ...
- 9.如何解决出现AXIOS的Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.
问题描述: 由于restful接口需要在头部header传递两个字段: Content-Type: application/jsonAccess-Token: 84c6635800b14e0eba4f ...
- vue2.0设置proxyTable使用axios进行跨域请求
这里请求的是知乎日报的api,由@izzyleung这位大神提供的,这是github地址. 在vue-cli构建的项目中先安装axios npm install axios -S 这里暂不考虑用vue ...
随机推荐
- pymysql连接mysql报错
pymysql模块操作数据库及连接报错解决方法 import pymysql sql = "select host,user,password from user" #想要执行 ...
- echart 柱状图背景色设置
方法一: series: [ { name: '1', type: 'bar', itemStyle: {//柱图背景色 color: '#111' }, data: list }, { name: ...
- Oracle数据库与MySQL的不同点
Oracle笔记 一. Oracle的启动和登录: 1.启动:通过启动Oracle的服务启动. OracleServiceORCL:核心服务,必须启动. OracleOraDb11g_home1 ...
- css3条纹进度条
新建div,取名progress,如下 <div class="progress"></div> 在里面插入条纹进度条,以及进度显示文本进度: <di ...
- BBS论坛 登录功能
四.登录功能 前端页面html代码: <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- IDEA 创建Spring cloud Eureka 注册中心
IDEA 创建Spring cloud Eureka 注册中心 一. 首先创建一个maven project Next之后填好groupId与artifactId,Next之后填好项目名与路径,点击F ...
- CentOS7.6下安装Oracle JDK
注:本教程使用XShell ssh到CentOS服务器,并使用root用户登录,如使用其他普通用户登录,请在命令前加sudo 1).在/usr/local/目录下(看个人情况)新建文件夹java用来存 ...
- Linux grep return code
The exit code is 1 because nothing was matched by grep. EXIT STATUS The exit status is 0 if selected ...
- vue中axios的封装以及简单使用
一.axios的封装 在vue中为了使用axios使用方便,不需要每一个模块进行导入,就需要对其进行封装: 1.新建http.js模块 import axios from 'axios' // 设置基 ...
- vscode编程nodejs初始安装
nodejs官网 http://nodejs.cn/ 1.安装nodejs,记得安装时勾选配置路径 在cmd中输入node,进去node环境即为安装成功. 2.安装vscode,并安装插件node e ...