找了半个互联网,愣是没找到一个官方,严谨的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. 16、Kubernetes搭建高可用集群
  2. php取汉字第一个字,php---------取汉字的第一个字的首字母
  3. 我的一些项目管理经验
  4. 【Qt】Qt学习资料汇总
  5. 查看历史操作记录(.bash_history)、修改文件时间
  6. 正几边形可以实现无缝拼接?
  7. STL浅析——序列式容器vector的数据结构
  8. android sdk httppost,android6.0SDK 删除HttpClient的相关类的解决方法
  9. 笔记本通过网口控制单片机_国产又推出笔记本:旋转屏,自带RS-232串口和网口,工程师专用...
  10. OPPO发力感知和计算领域,布局泛在服务未来
  11. linux 防火墙服务器,Linux服务器上适用的防火墙分析
  12. 摩拜女员工举报前端大佬性骚扰,擅用职权打压同事!
  13. 不可预料的压缩文件末端的解决方法
  14. 【无标题】程序员的一大步
  15. matlab在矿物加工中的应用,试述《矿物加工数学模型》在矿物加工中的作用
  16. 面试总结十一:MySQL
  17. ClickHouse入门到精通
  18. 更适合手写的办公本,办公参会时的效率神器,MAXHUB领效M6 Pro上手
  19. (更新时间)2021年6月4日 商城高并发秒杀系统(.NET Core版) 30-lua文件封装加载和执行
  20. HDU 4408 Minimum Spanning Tree 最小生成树计数

热门文章

  1. 实验及工程技术人员招聘 | 华南理工大学广州国际校区
  2. php机器人聊天对话框,仿机器人聊天窗口
  3. 车牌分割python_OpencvPython实现车牌字符分割
  4. 工具及方法 - Process Explorer以及类似工具,用来获取系统运行的进程信息
  5. nginx日志统计分析自动报表工具goaccess(推荐)
  6. Guitar Pro 如何添加装饰音?
  7. 成为一名优秀的数据分析师,所需要具备的能力有哪些
  8. CentOS7.2中vsftp安装、配置、卸载
  9. R语言使用choose函数计算排列组合:组合数(输入两个参数、combination)
  10. 终身受用的 48 条世界顶级思维