Axios的基本使用

介绍

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。在vue 中,用来发ajax请求,与后端交互。

  • 从浏览器中创建 XMLHttpRequests

  • 从 node.js 创建 http 请求

  • 支持 Promise API

  • 拦截请求和响应

  • 转换请求数据和响应数据

  • 取消请求

  • 自动转换 JSON 数据

  • 客户端支持防御 XSRF

axios的get请求

// 为给定 ID 的 user 创建请求
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
}); $.ajax({
url:'',
type'get',
success:function(data){
},
error:function(err){
}
})

aixos的post请求

axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

axios的默认配置

导入axios模块,然后直接用axios引用的方式,只适合该组件,其他组件要用,又要导入很麻烦。

未来以后axios是一个局部作用域的那么我们可以通过

Vue.prototype.$axios = axios;

此时我们就可以在任意组件中通过this.$axios获取到当前的axios实例

默认配置URL

axios.defaults.baseURL = 'http://127.0.0.1:8800'

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app"></div> <script type="text/javascript" src="./node_modules/vue/dist/vue.js"></script>
<!--发送ajax请求,需先引入模块-->
<script type="text/javascript" src="./node_modules/axios/dist/axios.js"></script>
<script type="text/javascript">
var App = {
data() {
return {
msg: ''
}
},
template: `
<div>
<button @click = 'sendAjax'>发Get</button>
<div v-html = 'msg'></div>
<button @click = 'sendAjaxByPost'>发post请求</button>
</div>
`,
methods: {
sendAjax() {
// 发送get请求
axios.get('http://127.0.0.1:8800/4')
.then(res => { // res 是返回的对象
console.log(res);
console.log(res.data); // 返回对象里面的数据
console.log(typeof res.data);
this.msg = res.data;
})
.catch(err => { // 捕捉错误信息
console.log(err);
}) },
sendAjaxByPost() {
var params = new URLSearchParams();
params.append('name', 'alex');
axios.post('http://127.0.0.1:8800/create', params)
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
})
}
}
};
new Vue({
el: "#app",
data() {
return {}
},
components: {
App
},
template: `<App></App>`
}) </script> </body>
</html>

基本使用

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="./node_modules/vue/dist/vue.js"></script>
<script type="text/javascript" src="./node_modules/axios/dist/axios.js"></script> <!-- vue和axios都是全局的对象 未来 axios会成为局部作用域-->
<script type="text/javascript"> // 挂载 Vue.prototype.$axios = axios; 使用插件
Vue.prototype.$axios = axios; // 后面直接使用 this.$axios 调用 // 配置公共的url
axios.defaults.baseURL = 'http://127.0.0.1:8800';
var App = {
data() {
return {
msg: '',
datas: []
}
},
template: `
<div>
<button @click = 'sendAjax'>发Get</button>
<div v-html = 'msg'></div>
<button @click = 'sendAjaxByPost'>发post请求</button>
{{datas}}
</div>
`,
methods: {
sendAjax() {
// 发送get请求
this.$axios.get('/')
.then(res => {
console.log(res.data);
console.log(typeof res.data);
this.msg = res.data; })
.catch(err => {
console.log(err);
}) },
sendAjaxByPost() {
var params = new URLSearchParams();
params.append('name', 'alex'); this.$axios.post('/create', params)
.then( res => { console.log(this);
console.log(res);
this.datas = res; // 将返回的结果,更改到 data
})
.catch(err => {
console.log(err);
})
}
}
}; new Vue({
el: "#app",
data() {
return {}
},
components: {
App
},
template: `<App />`
}) </script> </body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="./node_modules/vue/dist/vue.js"></script>
<script type="text/javascript" src="./node_modules/axios/dist/axios.js"></script> <!-- vue和axios都是全局的对象 未来 axios会成为局部作用域-->
<script type="text/javascript"> // 挂载 Vue.prototype.$axios = axios; 使用插件
Vue.prototype.$axios = axios; // 后面直接使用 this.$axios 调用 // 配置公共的url
axios.defaults.baseURL = 'http://127.0.0.1:8800';
var App = {
data() {
return {
msg: '',
datas: []
}
},
template: `
<div>
<button @click = 'sendAjax'>发Get</button>
<div v-html = 'msg'></div>
<button @click = 'sendAjaxByPost'>发post请求</button>
{{datas}}
</div>
`,
methods: {
sendAjax() {
// 发送get请求
this.$axios.get('/')
.then(res => {
console.log(res.data);
console.log(typeof res.data);
this.msg = res.data; })
.catch(err => {
console.log(err);
}) },
sendAjaxByPost() {
var params = new URLSearchParams();
params.append('name', 'alex'); this.$axios.post('/create', params)
.then( res => { console.log(this);
console.log(res);
this.datas = res; // 将返回的结果,更改到 data
})
.catch(err => {
console.log(err);
})
}
}
}; new Vue({
el: "#app",
data() {
return {}
},
components: {
App
},
template: `<App />`
}) </script> </body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="./node_modules/vue/dist/vue.js"></script>
<script type="text/javascript" src="./node_modules/axios/dist/axios.js"></script> <!-- vue和axios都是全局的对象 未来 axios会成为局部作用域-->
<script type="text/javascript"> // 挂载 Vue.prototype.$axios = axios; 使用插件
Vue.prototype.$axios = axios; // 后面直接使用 this.$axios 调用 // 配置公共的url
axios.defaults.baseURL = 'http://127.0.0.1:8800';
var App = {
data() {
return {
msg: '',
datas: []
}
},
template: `
<div>
<button @click = 'sendAjax'>发Get</button>
<div v-html = 'msg'></div>
<button @click = 'sendAjaxByPost'>发post请求</button>
{{datas}}
</div>
`,
methods: {
sendAjax() {
// 发送get请求
this.$axios.get('/')
.then(res => {
console.log(res.data);
console.log(typeof res.data);
this.msg = res.data; })
.catch(err => {
console.log(err);
}) },
sendAjaxByPost() {
var params = new URLSearchParams();
params.append('name', 'alex'); this.$axios.post('/create', params)
.then( res => { console.log(this);
console.log(res);
this.datas = res; // 将返回的结果,更改到 data
})
.catch(err => {
console.log(err);
})
}
}
}; new Vue({
el: "#app",
data() {
return {}
},
components: {
App
},
template: `<App />`
}) </script> </body>
</html>

默认配置

Axios的基本使用的更多相关文章

  1. 为什么axios请求接口会发起两次请求

    之前在使用axios发现每次调用接口都会有两个请求,第一个请求时option请求,而且看不到请求参数,当时也没注意,只当做是做了一次预请求,判断接口是否通畅,但是最近发现并不是那么回事. 首先我们知道 ...

  2. axios基本用法

    vue更新到2.0之后,作者就宣告不再对vue-resource更新,而是推荐的axios,前一段时间用了一下,现在说一下它的基本用法. 首先就是引入axios,如果你使用es6,只需要安装axios ...

  3. Axios、Lodash、TweenJS

    Axios是一个基于promise的HTTP库 http://chuansong.me/n/394228451820 Lodash是一个JavaScript的函数工具集 http://www.css8 ...

  4. axios全攻略

    随着 vuejs 作者尤雨溪发布消息,不再继续维护vue-resource,并推荐大家使用 axios 开始,axios 被越来越多的人所了解.本来想在网上找找详细攻略,突然发现,axios 的官方文 ...

  5. 抛弃vue-resource拥抱axios

    vue-resource用法 import Vue from 'vue' import VueResource from 'vue-resource' Vue.use(VueResource) 是不是 ...

  6. Vue+axios 实现http拦截及路由拦截

    现如今,每个前端对于Vue都不会陌生,Vue框架是如今最流行的前端框架之一,其势头直追react.最近我用vue做了一个项目,下面便是我从中取得的一点收获. 基于现在用vue+webpack搭建项目的 ...

  7. vue使用Axios做ajax请求

    vue2.0之后,就不再对vue-resource更新,而是推荐使用axios 1. 安装 axios $ npm install axios 或 $ bower install axios 2. 在 ...

  8. vue全家桶(Vue+Vue-router+Vuex+axios)(Vue+webpack项目实战系列之二)

    Vue有多优秀搭配全家桶做项目有多好之类的咱就不谈了,直奔主题. 一.Vue 系列一已经用vue-cli搭建了Vue项目,此处就不赘述了. 二.Vue-router Vue的路由,先献上文档(http ...

  9. 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 ...

  10. vue2.0设置proxyTable使用axios进行跨域请求

    这里请求的是知乎日报的api,由@izzyleung这位大神提供的,这是github地址. 在vue-cli构建的项目中先安装axios npm install axios -S 这里暂不考虑用vue ...

随机推荐

  1. 使用Ping来做等待的时间计算

    利用ping两次发送消息之间的间隔时间.ping在发送多个消息时,在得到上一次消息的回应后,它会再等待1秒的时间才发送下一次消息,而这个回应时间因机型.系统和网络配置而不同,其中IP地址尤其关键,只有 ...

  2. as3 去掉字符串空白问题

    去掉内容的所有空白 function trim(str:String):String { })/g,""); } //[ ]内是一个中文空格一个英文空格 {}是说匹配一个到多个 / ...

  3. linux文件格式转换:<U+FEFF> character showing up in files. How to remove them?

    You can easily remove them using vim, here are the steps: 1) In your terminal, open the file using v ...

  4. java多线程实例(2)

    public class ThreadDemo05 { public static void main(String args[]) { // 四个售票点应该控制同一个资源 Demo d = new ...

  5. 【Java】JVM(二)、Java垃圾收集算法

    一.标记-清除算法 算法主要分为两个步骤 1. 标记: 遍历所有的 GC Roots, 然后标记所有可达对象为存活对象 2. 清除: 遍历堆中所有对象,然后将没有标记的对象清除. 存在不足: 1. 效 ...

  6. hadoop无法启动常见原因

    1.Could not chdir to home directory /home/USER: Permission denied 启动datanode时会报这个错误,尝试利用ssh登录datanod ...

  7. Java实现聚类算法k-means

    2016-07 java简单实现聚类算法 但是有一个小问题,,,,我其实每次迭代之后(就是达不到我的收敛标准之前,聚类中心的误差达不到指定小的时候),虽然重新算了聚类中心,但是其实我的那些点并没有变, ...

  8. fusioncharts Y轴不显示中文的解决方法(转载)

    使用fusionChart主要是被其界面吸引了,各类图表都很好看,下载以后文档也很周全,支持的语言也很多种 ,容易上手.fusionChart工作原理主要是通过后台传xml数据源给报表前台flash ...

  9. linux下xampp(apache)中配置域名访问,以及遇到的问题

    xampp中apache使用域名访问 一.首先找到/opt/lampp/etc/httpd.conf: # Virtual hosts Include etc/extra/httpd-vhosts.c ...

  10. oracle的分析函数over(Partition by...) 及开窗函数

        over(Partition by...) 一个超级牛皮的ORACLE特有函数. oracle的分析函数over 及开窗函数一:分析函数overOracle从8.1.6开始提供分析函数,分析函 ...