axios简单使用
介绍
我在使用vue的时候使用到了axios,vue 1.0的版本作者推荐使用vue-resource,到了vue 2.0作者建议使用axios,此篇文章只是我在使用axios时候做的笔记,我遇到的一个些坑,如需查看axios详细api的使用文档请看官网https://www.npmjs.com/package/axios
Get,Delete,Head简单使用
get(url: string, config?: AxiosRequestConfig): AxiosPromise;
delete(url: string, config?: AxiosRequestConfig): AxiosPromise;
head(url: string, config?: AxiosRequestConfig): AxiosPromise
这三个方法使用方式属于同一类型,Get方法使用示例如下,其他两个同理
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
}); // Optionally the request above could also be done as
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Post,Put,Patch简单使用
post(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
put(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
patch(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
这三个方法使用方式属于同一类型,Post方法使用示例如下,其他两个同理:
axios.post(url,JSON.stringify(requestdata),{ headers: { 'Content-Type': 'application/json' },data:{}}).then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
这里需要注意的是data:{} 必须添加这个,即使data里面是空,headers是添加http表头的
axios post上传表单的时候,需要注意的是 headers: { 'Content-Type': 'application/x-www-form-urlencoded' 这个值,否则后台是接收不到值的
var formData = new FormData();
formData.append('file', this.$refs.fileData['files'][0], this.$refs.fileData['files'][0].name);
formData.append('applicationName', this.form.applicationName); axios.post(url, formData, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
.then(response => {
console.log(res.data)
})
.catch(error => {
console.log(error)
});
axios简单使用的更多相关文章
- vue axios 简单封装以及思考
先安装 axios npm install axios axios的详细介绍以及用法 就不多说了请 移步 github ➡️ https://github.com/axios/axios 下面是简单 ...
- axios 简单常用笔记
简单认知: 1.config.data是POST参数,config.params是GET参数 axios(url, [config]) + qs + application/x-www-form-ur ...
- axios简单了解
简单介绍 axios是基于客户端的promise,面向浏览器和nodejs 特色 浏览器端发起XMLHttpRequests请求 node端发起http请求 支持Promise API 监听请求和返回 ...
- axios简单封装
写在最前面 新手前端刚刚接触vue,感觉真的好用.项目中需要使用axios,然后学习了一下.借鉴网上一些大佬的经验,现在分享一下axios的简单封装,如果有什么错误的地方,请大家指出. axios安装 ...
- axios简单理解
发起一个GET请求 直接使用axios('/user')方法,axios()方法默认为GET方式 axios(’/user/12345’); 使用axios.get()方法,参数直接写以?key=va ...
- vue mock(模拟后台数据) +axios 简单实例(二)
需装上axios,build文件夹中webpack.dev.conf.js文件添加上vue mock配置的东东, 如,继(一) //组件<template> <div> &l ...
- axios简单介绍
axios的配置,get,post,axiso的同步问题解决 一.缘由 vue-resoure不更新维护,vue团队建议使用axios. 二.axios安装 1.利用npm安装npm install ...
- axios 简单二次封装
import axios from 'axios' import { Message } from 'element-ui'; // 设置baseURL //axios.defaults.baseUR ...
- vue axios简单配置
参考:https://www.cnblogs.com/sophie_wang/p/7844119.html 1. 安装 npm install axios 2. main.js import axio ...
随机推荐
- python3 xml模块
一.简介 xml是实现不通语言或程序之间进行数据交换的协议,可扩展标记语言,标准通用标记语言的子集.是一种用于标记电子文件使其具有结构性的标记语言.xml格式如下,是通过<>节点来区别数据 ...
- delphi 的 ORM 框架
delphi 的 ORM 框架,收集如下: 1.EntityDac https://www.devart.com/entitydac/download.html 2.mORMot The ...
- 修改主机IP地址
IP:由192.168.1.1~192.168.1.254. 先使用电脑以动态IP的方式自动获取ip地址联网,然后通过以下方法查询子网掩码和默认网关地址:
- cef_binary_3.2623.1401.gb90a3be
这个资源现在很难搜索到 分享给大家 http://www.ceffans.com/forum.php?mod=viewthread&tid=9 http://pan.baidu.com/sha ...
- $Django 客户端->wsgi->中间组件->urls->views(model,template) 总结+补充(事物,choices,inclusion_tag)!
1 HTTP协议:(重点) -请求 -请求首行 -GET /index HTTP/1.1 \r\n -请求头部 -key:value------>\r\n分割 _ke ...
- PDF阅读器中如何改变线条颜色、线宽和线型等
1.打开PDF阅读器过后,点击菜单栏"注释"这一栏,找到绘图区域中的线条或铅笔: 2.分别用线条和铅笔随意画两条直线,然后选中直线,右键点击直线并打开"属性": ...
- auth模块(登录验证)
settings:'django.contrib.auth.middleware.AuthenticationMiddleware',#这个是认证的中间件,认证成功的话,就可以把这个用户user封装到 ...
- 金九银十中,看看这31道Android面试题
阅读目录 1.如何对 Android 应用进行性能分析 2.什么情况下会导致内存泄露 3.如何避免 OOM 异常 4.Android 中如何捕获未捕获的异常 5.ANR 是什么?怎样避免和解决 ANR ...
- IntelliJ IDEA使用教程 (总目录篇)
注:本文来源于< IntelliJ IDEA使用教程 (总目录篇) > IntelliJ IDEA使用教程 (总目录篇) 硬件要求 IntelliJ IDEA 的硬件要求 安装包云 ...
- OCM 学习练习题目
1:数据安装操作练习:考试题目 1: Creating a database & Server Configuration --[101]-- #创建数据库 1. Create the dat ...