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 ...
随机推荐
- qemu中使用9p,支持host和guest中共享目录【转】
转自:https://blog.csdn.net/ayu_ag/article/details/52956351 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csd ...
- 广联达 BIM5D 云平台---《建筑信息模型标准》解读
广联达 BIM5D 云平台: 1.用户管理: https://account.glodon.com/info 2.模型使用: http://bim5d-hunan.glodon.com/api/v ...
- Linux 两组信号对比(关闭和停止进程信号)
之前看信号的时候,没有太注意不同信号的对比.今天再次看到的时候,突然感觉对一些信号,非常相似,乃至非常容易混淆.今天周末就抽空总结一下. 一.关闭进程信号 常见的4中关闭进程信号是SIGKILL,SI ...
- SQL Server代码段
1.cast和convert ' as int) -- 123 ') -- 123 select CAST(123.4 as int) -- 123 select CONVERT(int, 123.4 ...
- MD5算法工具类
抽时间写了一个算法工具类,目前支持的算法有SHA1,SHA256,SHA384,SHA512,MD5,同时支持获取文件的MD5值. 使用方法如下: 获取字符串的MD5值 String str= Alg ...
- Stetho简化Android调试(二)
Stetho简化Android调试(一) 一文中讲述了如何使用Stetho结合Chrome远程调试Android App. Stetho给我们调试带来很大的便利,效率显著提升的同时也产生一个问题:如果 ...
- iOS 去除百度地图下方的 logo
UIView *mView = _mapView.subviews.firstObject; for (id logoView in mView.subviews) { if ([logoView ...
- Confluence 6 数据库问题解除
有关数据库相关的问题,请参考 Database Troubleshooting 中的内容. 希望获得更多的帮助,请参考 Troubleshooting Problems and Requesting ...
- Android adb from work ---one
Write in front,You need to know some information first fastboot -help or adb -help (You can view the ...
- vue的多选框