vue--axios发送请求

摘要:
ImportAxiosfrom'axios'/*使用轴*/Vue。原型$http=axis;})Catch(function(error){console.log(error);id=123&//Arrow function axios.get(urls).then((res)=>'Zhang San'}})。然后(function(res){console.log(res);

首先安装: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发送请求》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Oracle SQL外连接linux查看CPU高速缓存(cache)信息下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Apache环境下强制http跳转至https的配置总结

一. 简单实例介绍一般来说,apache配置好http和https后,如果想要做http强转到https,需要设置url重定向规则,大致需要下面几个步骤即可完成配置: 1)在httpd.conf文件里使下面模块生效 [root@back ~]# cat /usr/local/apache/conf/httpd.conf ..... LoadModule s...

初学爬虫之访问goole网页与爬取中国大学排名。

Requests库get()函数访问google网页20次。 1.Requests模块介绍: Requests 是使用 Apache2 Licensed 许可证的 HTTP 库。用 Python 编写,真正的为人类着想。 Python 标准库中的 urllib2 模块提供了你所需要的大多数 HTTP 功能,但是它的 API 太渣了。它是为另一个时代、另一个...

php 面试题

1. 写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名  例如: http://www.phpddt.com/abc/de/fg.php?id=1 需要取出 php 或 .php $url = 'http://www.baidu.com/wang/liu/4.php?i=90'; $urlArr = parse_url($url); $ex...

linux svn命令具体解释

检測是否安装svn:svnserve --version svn服务的关闭:killall svnserve 创建svn库:svnadmin create /opt/svn/repos 配置自己主动启动:把svnserve -d -r /opt/svn/repos 放在/etc/rc.d/rc.local最后一行 启动svn: svnserve -d -...

JS URL参数传递 谷歌乱码解决

//第一个页面 var name=encodeURIComponent("参数"); var url="test1.html?name="+name; //第二个页面 var name=GetUrlParameters("name"); // 获取参数方法 name=decodeURIComponent(name); //encodeURICompon...

百度地图在前端开发中的运用

一、安装及引入 1、在vue中安装依赖 $ npm install vue-baidu-map --save 在vue脚手架main.js中引入 import BaiduMap from 'vue-baidu-map' Vue.use(BaiduMap, { ak: 'Yo8oGhNGslHc4B8Qs8EWI4BvU3Qt4Zla' }); ...