如何在 axios 中设置标题和选项?

编程入门 行业动态 更新时间:2024-10-27 20:36:24
本文介绍了如何在 axios 中设置标题和选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用 Axios 执行这样的 HTTP 帖子:

I use Axios to perform an HTTP post like this:

import axios from 'axios' params = {'HTTP_CONTENT_LANGUAGE': self.language} headers = {'header1': value} axios.post(url, params, headers)

这是正确的吗?或者我应该这样做:

Is this correct? Or should I do:

axios.post(url, params: params, headers: headers)

推荐答案

有几种方法可以做到这一点:

There are several ways to do this:

  • 对于单个请求:

  • For a single request:

let config = { headers: { header1: value, } } let data = { 'HTTP_CONTENT_LANGUAGE': self.language } axios.post(URL, data, config).then(...)

  • 用于设置默认全局配置:

  • For setting default global config:

    axios.defaults.headers.post['header1'] = 'value' // for POST requests axios.defaults.headersmon['header1'] = 'value' // for all requests

  • 用于在 axios 实例上设置为默认值:

  • For setting as default on axios instance:

    let instance = axios.create({ headers: { post: { // can be common or any other method header1: 'value1' } } }) //- or after instance has been created instance.defaults.headers.post['header1'] = 'value' //- or before a request is made // using Interceptors instance.interceptors.request.use(config => { config.headers.post['header1'] = 'value'; return config; });

  • 更多推荐

    如何在 axios 中设置标题和选项?

    本文发布于:2023-10-09 16:56:59,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1476254.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:选项   标题   如何在   axios

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!