1
 
1
2
3
4
5
6
7
8
9
10
// axios 请求  在main.js里边写入
import Axios from 'axios'
 
// 配置请求信息
var $http = Axios.create({
  baseURL: '配置路径',
  timeout: '3000',  //超时时间
  headers: {'X-Custom-Header': 'foobar'}  //请求头
})
Vue.prototype.$http = $http
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 方法调用,回调,post , 基于es6 promise
  methods: {
    getNum: function() {
      var data = { TenantID: "0"}
      this.$http.post("请求路径-方法1", data ) .then(res => {
          console.log(res) //返回方法1的值
                var data1 =  { TenantID: res.data.Code } //方法1 的返回值作为参数传递 res.data.Code
                this.$http.post("请求路径-方法2", data1 )
                .then(res1 => {
                    console.log(res.data) //同样可以使用方法1的返回值
                    console.log(res1)  //返回方法2的值
                })
            })
            .catch(function(error) {
                console.log(error)
        })
    }
//需要使用箭头函数,否则 this.$http 中的this 指向window,会报 $http undefined
//另外也可以在当前组件 import axios from 'axios'  引入axios 包
//将 this.$http 替换 为 axios  结果一样

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
axios 拦截器
 
你可以截取请求或响应在被 then 或者 catch 处理之前
 
//添加请求拦截器
axios.interceptors.request.use(function(config){
     //在发送请求之前做某事
     return config;
   },function(error){
     //请求错误时做些事
     return Promise.reject(error);
   });
  
//添加响应拦截器
axios.interceptors.response.use(function(response){
     //对响应数据做些事
     return response;
   },function(error){
     //请求错误时做些事
     return Promise.reject(error);
   });<br>
以后可能需要删除拦截器。
var myInterceptor = axios.interceptors.request.use(function () {/*...*/});
axios.interceptors.request.eject(myInterceptor);<br>
你可以将拦截器添加到axios的自定义实例。
var instance = axios.create();
instance.interceptors.request.use(function () {/*...*/});<br>
处理错误
axios.get('/ user / 12345')
   .catch(function(error){
     if(error.response){
       //请求已发出,但服务器使用状态代码进行响应
       //落在2xx的范围之外
       console.log(error.response.data);
       console.log(error.response.status);
       console.log(error.response.headers);
     } else {
       //在设置触发错误的请求时发生了错误
       console.log('Error',error.message);
     }}
     console.log(error.config);
   });
您可以使用validateStatus配置选项定义自定义HTTP状态码错误范围。
 
axios.get('/ user / 12345',{
   validateStatus:function(status){
     return status < 500; //仅当状态代码大于或等于500时拒绝
   }}
})

vue 如何发起网络请求 之 axios的更多相关文章

  1. android4.0 HttpClient 以后不能在主线程发起网络请求

    android4.0以后不能在主线程发起网络请求,该异步网络请求. new Thread(new Runnable() { @Override public void run() { // TODO ...

  2. iOS swift HandyJSON组合Alamofire发起网络请求并转换成模型

    在swift开发中,发起网络请求大部分开发者应该都是使用Alamofire发起的网络请求,至于请求完成后JSON解析这一块有很多解决方案,我们今天这里使用HandyJSON来解析请求返回的数据并转化成 ...

  3. 使用axios优雅的发起网络请求

    原文链接:https://www.jianshu.com/p/73585303fdc0 公司项目使用了vue作为技术栈,便理所应当地使用了官方推荐的axios进行网络请求,这里记录下axios的封装方 ...

  4. vue(24)网络请求模块axios使用

    什么是axios Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 主要的作用:axios主要是用于向后台发起请求的,还有在请求中做更多是可控功能. a ...

  5. 木马——本质就是cs socket远程控制,反弹木马是作为c端向外发起网络请求

    摘自:http://kczxsp.hnu.edu.cn/upload/20150504165623705.pdf 里面对于木马的实验过程写得非常清楚,值得一看.   木马是隐藏在正常程序中的具有特殊功 ...

  6. uniapp 发起网络请求

    推荐下我写的uni-http 创建http-config.js import Vue from 'vue' const BASE_URL = 'http://xxx.com'; if (process ...

  7. thinkphp发起网络请求

    常规做法使用CURL方法: private function http_request($url,$data = null,$headers=array()){ $curl = curl_init() ...

  8. 十. Axios网络请求封装

    1. 网络模块的选择 Vue中发送网络请求有非常多的方式,那么在开发中如何选择呢? 选择一:传统的Ajax是基于XMLHttpRequest(XHR) 为什么不用它呢?非常好解释配置和调用方式等非常混 ...

  9. React Native网络请求

    很多移动应用都需要从远程地址中获取数据或资源.你可能需要给某个REST API发起POST请求以提交用户数据,又或者可能仅仅需要从某个服务器上获取一些静态内容--以下就是你会用到的东西.新手可以对照这 ...

随机推荐

  1. 自动生成DTO(Sugar框架)

    step1:启动api项目 step2:使用postman工具,填上接口地址http://localhost:7788/api/automapper/AutoMapperSuper step3:表格数 ...

  2. Programming | 变量名的力量

    命名准则 变量名要完全,准确的描述变量所代表的事物,一般而言,对变量的描述就是最佳的变量名.避免x,temp,i等泛泛而谈的变量名. 比如对于矩阵的循环,matrix[row][col]就比m[i][ ...

  3. 基本包装类型Boolean、Number、String特性及常用方法

    基本包装类型:Boolean.Number.String 一.String 字符串常用方法 1.indexOf()  lastIndexOf()  返回相应字符的索引号 2.slice(index1, ...

  4. nginx压力测试webbench

    下载压力测试工具webbench wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz 安装依赖包 yum -y in ...

  5. P3718 [AHOI2017初中组]alter

    贪心+二分答案 二分最终答案长度 主要问题在check上 ~~我代码写得巨丑,大家还是不要看我的代码了~~ ------------ 1:当mid大于1的时候,贪心策略是这样的: 当前连续的长度大于m ...

  6. Spring Boot 整合 ActiveMQ

    依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...

  7. vuex基础知识总结

    项目中要求添加vuex,根据学习我这个小白总结了一点自己的心得,供大家参考 在学习之前,要知道两件事 为什么要用vuex?vuex要什么场景下应用? 简单点解释一下 1.项目中应用了vue脚手架之后, ...

  8. iOS7新特性-完美解决iOS7关于自定义导航条UIBarButtonItem偏移的问题

    前言: 本文由DevDiv社区@Vincent 原创,转载请注明出处! http://www.devdiv.com/iOS_iPhone-ios_ios_uibarbuttonitem_-thread ...

  9. C++ const修饰不同类型的用法

    const取自constant的缩写,本意是不变的,不易改变的意思 一.修饰普通变量 const int a = 7; int b = a;         //正确 a = 8;           ...

  10. elasticDump的安装使用

    官方地址:官方地址:https://github.com/taskrabbit/elasticsearch-dump 安装方式如下:安装NodeJS下载源码:wget http://nodejs.or ...