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. Spring boot 执行jar文件 方式

    项目jar包名wxo.jar 清理,打包,跳过测试(不测试) mvn clean package -Dmaven.test.skip=true 后台执行(默认环境) nohup java -jar w ...

  2. Asp.Net MVC参考资料

    Every day up!!!!!! 1.无废话MVC入门教程 2.MVC快速入门 3.MVC小牛之路 4.Web API强势入门指南 5.全网最全的mvc汇总 6.MVC5+EF6+Bootstra ...

  3. ubuntu查询可用安装包

    当使用apt-get install packages时,如果不知道安装包的具体名称,可以使用关键字进行搜索,使用:apt-cache search keywords

  4. 八月(The Summer is Gone)观后感

    第一次看到这部电影时觉得很亲近,黑白画面,国企改革的背景,浓浓的儿时画面感,原谅我只是一个三十不到的人,可能我比较早熟,对八九十年代还有些记忆,更早以前也通过电视.音乐.书籍等了解过一些,而那些听过又 ...

  5. springMVC学习记录3-拦截器和文件上传

    拦截器和文件上传算是springmvc中比较高级一点的内容了吧,让我们一起看一下. 下面先说说拦截器.拦截器和过滤器有点像,都可以在请求被处理之前和请求被处理之到做一些额外的操作. 1. 实现Hand ...

  6. 关于eval()函数处理后台返回的json数据

    对于服务器返回的JSON字符串,如果jquery异步请求没做类型说明,或者以字符串方式接受,那么需要做一次对象化处理,方式不是太麻烦,就是将该字符串放于eval()中执行一次.这种方式也适合以普通ja ...

  7. (转载)session token机制

    http://blog.chinaunix.net/uid-26642709-id-3061264.html 使用session token时,必须用struts2表标签库,不能用html 通过ses ...

  8. Servlet Request 请求转发

    request.getRequestDispatcher("logined.jsp").forward(request, response);    //登录用户在登录页面验证通过 ...

  9. scala -- 柯里化

    柯里化 柯里化是把接受多个参数的函数,变成接受一个单一参数的函数.并且返回接受剩余参数和返回结果的新函数. 就是一个逐次消元的过程. 当把函数的元全消掉,就得到了值. 值就是零元函数. 二元函数 f( ...

  10. Fiddler 捕获 nodejs 模拟的http请求

    1.设置Fiddler Tools->Options-> Connections Allow remote computers to connect: 2.nodejs 请求有多种 2.1 ...