首先安装:axios

$ npm install axios
$ cnpm install axios //taobao源
$ bower install axios
或者使用cdn:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

main.js里面引入:

import axios from 'axios'
/* 使用 axios */
Vue.prototype.$http=axios;

其他封装的 .js 文件里面引入:

import axios from 'axios'
/* 使用 axios */
Vue.prototype.$http=axios;

1、发送 get 请求

let urls = url+"/id/789";
axios.get(urls).then(function (response) {
console.log(response);
}).catch(function (error){
console.log(error);
});
// 箭头函数
axios.get(urls).then((res)=>{
console.log(res);
});

上面的get请求传递参数还不行:

// 传递参数 方法一
let urls = url+"?id=123&lpage=1";
axios.get(urls).then(function(res){
console.log(res);
});
// 箭头函数
axios.get(urls).then((res)=>{
console.log(res);
});

方法二:

// 传递参数 方法二
axios.get(url,{
params:{id:123,name:'张三'}
}).then(function(res){
console.log(res);
});
// 箭头函数
axios.get(url,{
params:{id:123,name:'张三'}
}).then((res)=>{
console.log(res);
});

2、发送post请求

axios.post(url,{name:'xiaoming'}).then(function(res){
console.log(res);
});

上面这个是在网上找到发送 POST 的请求的方法,确实也是可以发送请求,但是参数带不过去。

废了九牛二虎之力自己写了一个:

axios({
method:'post',
url:url,
data:{name:"aaa",id:1,age:20},
headers:{'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj){
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
}).then(function(res){
console.log(res);
}).catch(resp =>{
console.log(res);
});

使用箭头函数:

axios({
method:'post',
url:url,
data:{name:"aaa",id:1,age:20},
headers:{'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj){
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
}).then((res)=>{
console.log(res);
});

进行封装:

// 使用
axiosPost(url,{value:'不错'},function(res){
console.log(res);
});
// 封装 axio post请求
function axiosPost(url,data,fun){
axios({
method:'post',
url:url,
data:data,
headers:{'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj){
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
}).then((res)=>{fun(res);});
};

发送数据建议使用 Qs:

// axiosPost
axiosPost:function(url,data,fun){
axios({
method: 'post',
url:url,
data:Qs.stringify(data),
}).then((res)=>{fun(res);});
}

使用封装的请求:

this.axiosPost(url,data,function(res){
console.log(res);
});
// 使用箭头函数
this.axiosPost(url,data,(res)=>{
console.log();
});

vue--axios发送请求的更多相关文章

  1. Vue项目中使用Vuex + axios发送请求

    本文是受多篇类似博文的影响写成的,内容也大致相同.无意抄袭,只是为了总结出一份自己的经验. 一直以来,在使用Vue进行开发时,每当涉及到前后端交互都是在每个函数中单独的写代码,这样一来加大了工作量,二 ...

  2. vue-cli3.x中使用axios发送请求,配合webpack中的devServer编写本地mock数据接口(get/post/put/delete)

    vue-cli3.x中使用axios发送请求,配合webpack中的devServer编写本地mock数据接口(get/post/put/delete) 手把手式笔记 Axios配置 安装 axios ...

  3. Vue 爬坑之路(六)—— 使用 Vuex + axios 发送请求

    Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios ...

  4. Vue笔记:使用 axios 发送请求

    在Vue1.0的时候有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource. 关于为什么放弃推荐? -> 尤 ...

  5. vue中使用axios发送请求

    我们知道,vue2.0以后,vue就不再对vue-resource进行更新,而是推荐axios,而大型项目都会使用 Vuex 来管理数据,所以这篇博客将结合两者来发送请求 1.安装axios cnpm ...

  6. Vue 爬坑之路—— 使用 Vuex + axios 发送请求

    Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios  ...

  7. vue中采用axios发送请求及拦截器

    这几天在使用vue中axios发送get请求的时候很顺手,但是在发送post请求的时候老是在成功的回调函数里边返回参数不存在,当时就纳闷了,经过查阅资料,终于得到了解决方案,在此做一总结: 首先我们在 ...

  8. vue项目使用axios发送请求让ajax请求头部携带cookie

    最近做vue项目时遇到登录权限问题,登录以后再发送的请求头部并没有携带登录后设置的cookie,导致后台无法校验其是否登录.检查发现是vue项目中使用axios发送ajax请求导致的.查看文档得知ax ...

  9. vue2.0项目实战(3)使用axios发送请求

    在Vue1.0的时候有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource. 关于为什么放弃推荐? -> 尤 ...

  10. vuejs+axios发送请求

    Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios  ...

随机推荐

  1. RMAN:简单的duplicate创建新数据库 for 12c+

    构建参数文件 *.db_name='test2' ##### 需要注意的地方,和rman的duplicate目标库一致 *.compatible='18.0.0' ##### 关键的地方,每个版本的模 ...

  2. Blender 界面操作

    1.旋转场景 使用鼠标中间键旋转整个场景.按住Shift键,再操作鼠标中间键则可平移整个场景. 2.数字键盘功能 数字键盘1.3.7,分别控制场景向前.向后.向上显示. 数字键盘5,可以在正射投影(O ...

  3. python的初始化运行了哪些?

    下面的3个print一个是在模块下面,一个是函数里面,一个是类名下面(不在方法里面) 1. 运行这段代码可以发现第3行和11行可以打印出来.第7行没有打印出来.所以可以放心,函数或者方法里面就算有错误 ...

  4. 分享一个有趣的代码,调用电脑中的api语音

    在文本文件中输入如下代码: set objTTS = CreateObject("sapi.spvoice") objTTS.speak("为啥我对象这么闹呢?" ...

  5. LeetCode[Array]----3Sum

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  6. Linux curl 命令下载文件

    引用自http://blog.csdn.net/wh211212/article/details/54285921 命令:curl 在Linux中curl是一个利用URL规则在命令行下工作的文件传输工 ...

  7. Splash args 属性

    args属性可以获取加载时配置的参数,一般我们只传入URL,如下,args.url 就相当于加载时配置的URL参数,我们把它赋值给 url 变量然后返回:

  8. Splash 对象属性

    args js_enabled resource_timeout images_enabled plugins_enabled scroll_position

  9. mybatis 之 parameterType="list"

    <!-- 根据货品编号查找货品图片地址,货品ID,商品ID,货品名称 --> <select id="getGoodsInfoByGoodsNo" paramet ...

  10. C/C++获取文件后缀名并且比较

    以下这段是VC中过去文件后缀名的方法 1.CString GetSuffix(CString strFileName) {         return strFileName.Right(strFi ...