vue中axios安装及使用

axios  基于HTTP客户端的浏览器和node.js

网址链接:https://github.com/axios/axios

1、特征:

  • 制作,使XMLHttpRequest从浏览器
  • 制作,使http来自node.js的请求
  • 支持承诺API
  • 拦截请求和响应
  • 转换请求和响应数据
  • 取消请求
  • JSON数据的自动转换
  • 客户端支持防止XSRF

2、安装:

Using npm:
$ npm install axios
Using bower:
$ bower install axios
Using yarn:
$ yarn add axios
Using cdn:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

3、配置使用:

post方法:
// Send a POST request
axios({
  method: ‘post‘,
  url: ‘/user/12345‘,
  data: {
    firstName: ‘Fred‘,
    lastName: ‘Flintstone‘
  }
});

get方法:
// GET request for remote image
axios({
  method: ‘get‘,
  url: ‘http://bit.ly/2mTM3nY‘,
  responseType: ‘stream‘
})
  .then(function (response) {
    response.data.pipe(fs.createWriteStream(‘ada_lovelace.jpg‘))
  });

4、响应模式

请求的响应包含以下信息。

{
  // `data` is the response that was provided by the server
  data: {},

  // `status` is the HTTP status code from the server response
  status: 200,

  // `statusText` is the HTTP status message from the server response
  statusText: ‘OK‘,

  // `headers` the headers that the server responded with
  // All header names are lower cased
  headers: {},

  // `config` is the config that was provided to `axios` for the request
  config: {},

  // `request` is the request that generated this response
  // It is the last ClientRequest instance in node.js (in redirects)
  // and an XMLHttpRequest instance in the browser
  request: {}
}

此处仅作交流学习,版权归原作者所有。

原文地址:https://www.cnblogs.com/w-yue/p/11792107.html

时间: 2024-07-30 20:25:09

vue中axios安装及使用的相关文章

聊聊 Vue 中 axios 的封装

聊聊 Vue 中 axios 的封装 axios 是 Vue 官方推荐的一个 HTTP 库,用 axios 官方简介来介绍它,就是: Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 作为一个优秀的 HTTP 库,axios 打败了曾经由 Vue 官方团队维护的 vue-resource,获得了 Vue 作者尤小右的大力推荐,成为了 Vue 项目中 HTTP 库的最佳选择. 虽然,axios 是个优秀的 HTTP 库,但是,直接在项目中使用并不是那

vue中axios的基本配置

vue中axios的基本配置 1.配置默认地址 axios.defaults.baseURL = ""; 2.发送数据详解 axios 使用 post 发送数据时,默认是直接把 json 放到请求体中提交到后端的.也就是说,我们的 Content-Type 变成了 application/json;charset=utf-8 ,这是axios默认的请求头content-type类型.但是实际我们后端要求的 'Content-Type': 'application/x-www-form-

vue 中axios 的基本配置和基本概念

ajax:异步请求,是一种无需再重新加载整个网页的情况下,能够更新部分网页的技术 axios:用于浏览器和node.js的基于promise的HTTP客户端 axios的安装: cmd命令行进入到vue项目下,执行npm install axios 然后执行提示npm install --save axios vue-axios 配置方法: 打开vue的编辑器,找到当前项目的main.js文件,输入: import axios from 'axios' Vue.prototype.axios =

vue中axios 配置请求拦截功能 及请求方式如何封装

main.js 中: import axios from '................/axios' axios.js 中: //axios.js import Vue from 'vue' import axios from 'axios' Vue.prototype.$http = axios //http request 封装请求头拦截器 axios.interceptors.request.use(config => { // console.log("request&quo

vue中axios的封装

第一步还是先下载axios cnpm install axios -S 第二步建立一个htttp.js import axios from 'axios'; import { Message } from 'element-ui'; axios.defaults.timeout = 5000; axios.defaults.baseURL =''; //http request 拦截器 axios.interceptors.request.use( config => { // const to

Vue中axios的使用技巧配置项详解

使用axios首先要下载axios模块包 npm install axios --save 其次需要在使用的文件中引入 import axios from 'axios' 一.调用axios常见两种方法(此处使用easy-mock模拟数据接口): //方法1 axios({ method: 'post', url:'http://easy-mock.com/mock/596077559adc231f357bcdfb/axios/test-post-axios' }) .then((respons

vue中axios设置

//设置默认全局baseURL axios.defaults.baseURL=process.env.BASE_API; //设置默认全局携带浏览器cookie axios.defaults.withCredentials=true; Vue.prototype.$http = axios; 原文地址:https://www.cnblogs.com/randomlee/p/10167306.html

vue中axios发送post请求,后端(@RequestParam)接不到参数

遇到的问题描述 :axios post 请求,后端接收不到参数. 我们的接口是java,用@RequestParam来接收前端的参数 解决方案:使用qs:axios中已经包含有qs,所以无需重新安装,直接引入就好 import Qs from 'qs'//引入qs let chedata = { data: encStr, sign: md5.hexMD5(che), timestamp: timestamp, } //chedata是我需要传递给后端的参数 console.log(Qs.str

vue中axios请求简单封装

1.http.js /**axios封装 * 请求拦截.相应拦截.错误统一处理 */ import axios from 'axios'; import QS from 'qs'; import { Toast } from 'vant'; import store from '../store/index' // 环境的切换 if (process.env.NODE_ENV == 'development') { axios.defaults.baseURL = '/api'; } else