博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
axios 请求嵌入请求_如何对每个Axios请求强制使用凭据
阅读量:2509 次
发布时间:2019-05-11

本文共 1582 字,大约阅读时间需要 5 分钟。

axios 请求嵌入请求

I was using Axios to interact with an API that set a JWT token.

我使用Axios与设置JWT令牌的API进行交互。

The API returned the token in a cookie and I quickly figured I needed to set withCredentials: true in the Axios options:

该API在cookie中返回了令牌,我很快就想出需要在Axios选项中设置withCredentials: true

import axios from 'axios'axios.post(API_SERVER + '/login', { email, password }, { withCredentials: true })

Otherwise the cookie would not be saved.

否则,cookie将无法保存。

I also needed to set it for every other request I made, to send the JWT token to the server:

我还需要为我提出的所有其他请求设置它,以将JWT令牌发送到服务器:

axios.get(API_SERVER + '/todos', { withCredentials: true })

Now, it’s ok for a few requests, but for many, you’d probably like to use a general configuration.

现在,可以接受一些请求,但是对于许多请求,您可能希望使用常规配置。

You can do it using the create() method to create a new Axios instance you’ll then use it in your requests:

您可以使用create()方法创建新的Axios实例,然后在请求中使用它:

import axios from 'axios'const instance = axios.create({  withCredentials: true})instance.get(API_SERVER + '/todos')

It’s also common to add a baseURL property:

添加baseURL属性也很常见:

import axios from 'axios'const instance = axios.create({  withCredentials: true,  baseURL: API_SERVER})instance.get('todos')

In React I used , and to configure withCredentials I used this code:

在React中,我使用 ,并使用以下代码配置withCredentials

import axios from 'axios'import useAxios, { configure } from 'axios-hooks'const instance = axios.create({  withCredentials: true,  baseURL: API_SERVER,})configure({ instance })const [{ data, loading, error }, refetch] = useAxios('todos')

翻译自:

axios 请求嵌入请求

转载地址:http://ramgb.baihongyu.com/

你可能感兴趣的文章
网络虚拟化技术(二): TUN/TAP MACVLAN MACVTAP
查看>>
MQTT协议笔记之mqtt.io项目HTTP协议支持
查看>>
(转)jQuery中append(),prepend()与after(),before()的区别
查看>>
Tecplot: Legend和图像中 Dashed/Dash dot/Long dash 等虚线显示没有区别的问题
查看>>
Python读取mysql数据,转为DataFrame格式并根据原TABLE中的COLUMNS指定columns,index
查看>>
简单版用户登录
查看>>
hdu 1250(大整数)
查看>>
hdu 1518(DFS+剪枝)
查看>>
win8 开发之旅(2) --连连看游戏开发 项目错误的总结
查看>>
视频转换工具ffmpeg
查看>>
一、 object c -基础学习第一天 如何定义一个类
查看>>
C#调用C++编译的DLL详解
查看>>
Kali Linux的安装
查看>>
node微信公众号开发---自动回复
查看>>
cuda小白基础教程
查看>>
the useful for loop
查看>>
我的大学生活-5-08-赵心宁
查看>>
黑马程序员-Java基础-反射
查看>>
bzoj1708[Usaco2007 Oct]Money奶牛的硬币(背包方案数dp)
查看>>
P2700逐个击破(并查集/树形dp)
查看>>