找了半个互联网,愣是没找到一个官方,严谨的axios简写方式设置headers的文档,那行,我规范的写一次吧。
下面介绍官方,家喻户晓的Request method aliases(简称)写法

Get

格式:axios.get(url[, config])

axios.get('/api/getUserinfo?user=sample',{headers: {'auth-jwt': window.sessionStorage.getItem('auth-jwt')}
})

Tips
当然你也可以让axios去构造url参数,详见下方我的附件

axios.get('/api/getUserinfo',{headers: {'auth-jwt': window.sessionStorage.getItem('auth-jwt')},params:{user: 'sample'}
})

Post

格式axios.post(url[, data[, config]])

axios.post('/api/getUserInfo',{user: 'sample'},{headers:{'auth-jwt': window.sessionStorage.getItem('auth-jwt')}
}),
其他请求类型,如此类推,点击上方查看官方文档链接

附config参数的全格式

url,methods可以省略

{// `url` is the server URL that will be used for the requesturl: '/user',// `method` is the request method to be used when making the requestmethod: 'get', // default// `baseURL` will be prepended to `url` unless `url` is absolute.// It can be convenient to set `baseURL` for an instance of axios to pass relative URLs// to methods of that instance.baseURL: 'https://some-domain.com/api/',// `transformRequest` allows changes to the request data before it is sent to the server// This is only applicable for request methods 'PUT', 'POST', 'PATCH' and 'DELETE'// The last function in the array must return a string or an instance of Buffer, ArrayBuffer,// FormData or Stream// You may modify the headers object.transformRequest: [function (data, headers) {// Do whatever you want to transform the datareturn data;}],// `transformResponse` allows changes to the response data to be made before// it is passed to then/catchtransformResponse: [function (data) {// Do whatever you want to transform the datareturn data;}],// `headers` are custom headers to be sentheaders: {'X-Requested-With': 'XMLHttpRequest'},// `params` are the URL parameters to be sent with the request// Must be a plain object or a URLSearchParams objectparams: {ID: 12345},// `paramsSerializer` is an optional function in charge of serializing `params`// (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)paramsSerializer: function (params) {return Qs.stringify(params, {arrayFormat: 'brackets'})},// `data` is the data to be sent as the request body// Only applicable for request methods 'PUT', 'POST', and 'PATCH'// When no `transformRequest` is set, must be of one of the following types:// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams// - Browser only: FormData, File, Blob// - Node only: Stream, Bufferdata: {firstName: 'Fred'},// syntax alternative to send data into the body// method post// only the value is sent, not the keydata: 'Country=Brasil&City=Belo Horizonte',// `timeout` specifies the number of milliseconds before the request times out.// If the request takes longer than `timeout`, the request will be aborted.timeout: 1000, // default is `0` (no timeout)// `withCredentials` indicates whether or not cross-site Access-Control requests// should be made using credentialswithCredentials: false, // default// `adapter` allows custom handling of requests which makes testing easier.// Return a promise and supply a valid response (see lib/adapters/README.md).adapter: function (config) {/* ... */},// `auth` indicates that HTTP Basic auth should be used, and supplies credentials.// This will set an `Authorization` header, overwriting any existing// `Authorization` custom headers you have set using `headers`.// Please note that only HTTP Basic auth is configurable through this parameter.// For Bearer tokens and such, use `Authorization` custom headers instead.auth: {username: 'janedoe',password: 's00pers3cret'},// `responseType` indicates the type of data that the server will respond with// options are: 'arraybuffer', 'document', 'json', 'text', 'stream'//   browser only: 'blob'responseType: 'json', // default// `responseEncoding` indicates encoding to use for decoding responses// Note: Ignored for `responseType` of 'stream' or client-side requestsresponseEncoding: 'utf8', // default// `xsrfCookieName` is the name of the cookie to use as a value for xsrf tokenxsrfCookieName: 'XSRF-TOKEN', // default// `xsrfHeaderName` is the name of the http header that carries the xsrf token valuexsrfHeaderName: 'X-XSRF-TOKEN', // default// `onUploadProgress` allows handling of progress events for uploadsonUploadProgress: function (progressEvent) {// Do whatever you want with the native progress event},// `onDownloadProgress` allows handling of progress events for downloadsonDownloadProgress: function (progressEvent) {// Do whatever you want with the native progress event},// `maxContentLength` defines the max size of the http response content in bytes allowedmaxContentLength: 2000,// `validateStatus` defines whether to resolve or reject the promise for a given// HTTP response status code. If `validateStatus` returns `true` (or is set to `null`// or `undefined`), the promise will be resolved; otherwise, the promise will be// rejected.validateStatus: function (status) {return status >= 200 && status < 300; // default},// `maxRedirects` defines the maximum number of redirects to follow in node.js.// If set to 0, no redirects will be followed.maxRedirects: 5, // default// `socketPath` defines a UNIX Socket to be used in node.js.// e.g. '/var/run/docker.sock' to send requests to the docker daemon.// Only either `socketPath` or `proxy` can be specified.// If both are specified, `socketPath` is used.socketPath: null, // default// `httpAgent` and `httpsAgent` define a custom agent to be used when performing http// and https requests, respectively, in node.js. This allows options to be added like// `keepAlive` that are not enabled by default.httpAgent: new http.Agent({ keepAlive: true }),httpsAgent: new https.Agent({ keepAlive: true }),// 'proxy' defines the hostname and port of the proxy server.// You can also define your proxy using the conventional `http_proxy` and// `https_proxy` environment variables. If you are using environment variables// for your proxy configuration, you can also define a `no_proxy` environment// variable as a comma-separated list of domains that should not be proxied.// Use `false` to disable proxies, ignoring environment variables.// `auth` indicates that HTTP Basic auth should be used to connect to the proxy, and// supplies credentials.// This will set an `Proxy-Authorization` header, overwriting any existing// `Proxy-Authorization` custom headers you have set using `headers`.proxy: {host: '127.0.0.1',port: 9000,auth: {username: 'mikeymike',password: 'rapunz3l'}},// `cancelToken` specifies a cancel token that can be used to cancel the request// (see Cancellation section below for details)cancelToken: new CancelToken(function (cancel) {})
}

axios 设置header相关推荐

  1. Nuxt使用cookies踩坑之设置axios的header

    情景介绍:公司中有一个类似单点登录的项目,主系统中有登录后username和token,存放在浏览器的cookies中,现在改造的子系统需要拿到这两个cookie值,再通过axios的设置header ...

  2. vue中axios改变header为application/x-www-form-urlencoded不起作用

    vue中axios改变header为application/x-www-form-urlencoded不起作用 axios默认的头是这个,一般get请求是这个头 config.headers['Con ...

  3. html设置 header,http设置header

    在阅读本文前,大家要有一个概念,在实现正常的TCP/IP 双方通信情况下,是无法伪造来源 IP 的,也就是说,在 TCP/IP 协议中,可以伪造数据包来源 IP ,但这会让发送出去的数据包有去无回,无 ...

  4. python flask 设置 header 响应体、响应头、状态码

    需求场景 在api设计中,基于restful的设计原则,一个http的响应应该包含执行的响应信息以及状态码. 例如:一个错误信息的响应信息应该包含内容以及返回对应的设计错误码. 在flask中如何制定 ...

  5. ajxs跨域 php_php设置header头允许ajax跨域请求

    在做项目的时候,我们有时候希望能够可以跨域进行请求,但是ajax访问php接口的时候,通常会报一个错误: Failed to load 你的网址/test.php: No 'Access-Contro ...

  6. 使用React和axios设置服务器端渲染的最简单方法

    by Simone Busoli 通过西蒙娜·布索利(Simone Busoli) 使用React和axios设置服务器端渲染的最简单方法 (The easiest way to set up ser ...

  7. Springboot应用中过滤器chain.doFilter后设置header无效

    Springboot应用中过滤器chain.doFilter后设置header无效 本文是在使用过滤器添加动态header过程中遇到设置header无效,经过研究源码而产生. 因为特殊需求,自定义的h ...

  8. 微信小程序wx.request请求接口需设置header: { accept: */*,content-type: application/json },

    开始使用header: { "content-type": "application/json" },发送wx.request请求,报错,后台使用 Nancy ...

  9. 关于http和https允许请求(Nginx)设置header问题

    最近遇到了一个问题,接口用户拦截过滤的信息(token信息扔到header中处理)在本地或测试环境上面跑是没有问题的,但是切换到正式服务器上面,却没有获取到header里面的token信息,导致无法执 ...

最新文章

  1. 每日一皮:某程序员对书法十分感兴趣。一日饭后突生雅兴...
  2. VTK:几何对象之Triangle
  3. 项目受源代码管理。向源代码管理注册此项目时出错。建议不要对此项目进行任何更改...
  4. mysql 安装软件无法启动不了_Mysql 安装服务无法启动解决方案与使用的一般使用指令...
  5. CNN-2: AlexNet 卷积神经网络模型
  6. 干货 | 如何系统学习 C 语言?
  7. 单目深度估计方法:现状与前瞻
  8. python随机数列_Python2随机数列生成器简单实例
  9. leetcode python3 简单题14. Longest Common Prefix
  10. 基于BAE微信公众账号管理系统答辩PPT免费下载
  11. string类常用方法3
  12. 开启3389的方法记录
  13. java7 32位官方下载_【java7】64位+32位官方下载
  14. Win10查看电脑上次的开机时间
  15. 2017暴雪php,动视暴雪2017Q4财报 开启全新里程碑
  16. 3dmax顶点动画导入unity_从3dmax如何导入物体到unity3d
  17. 云服务器配置代理服务
  18. 用大白话说说JavaWeb相关技术
  19. 用sealed修饰的类有什么特点
  20. 杀死我们的,大都是光鲜甚至美好的东西

热门文章

  1. docker 容器防火墙设置
  2. OpenWRT 镜像
  3. ios 15后自定义navigationcontroller背景和title颜色
  4. ACL最全详解:原理及作用、分类及特点、配置及需求
  5. vue中Mixin和extends详解
  6. JS处理32位整型位运算
  7. html怎么给图片命名,如何给照片命名的8种方法
  8. vi编辑器的基本用法
  9. 腾讯云CloudPages建站模板搭建网站教程
  10. python中元组支持双向索引吗_Python 元组支持双向索引