要在 React 中使用 Ant Design,并在请求头中携带 Basic Auth,可以按照以下步骤进行操作:

  1. 首先,确保你的 React 项目已经安装了 Ant Design 和 Axios。可以运行以下命令进行安装:

npm install antd axios


2. 在你的 React 组件中,导入所需的库:

```jsx
import React from 'react';
import { Button } from 'antd';
import axios from 'axios';
  1. 在你的组件中创建一个函数,用于处理点击事件并发送带有 Basic Auth 的请求:

const handleButtonClick = () => { const username = 'your_username'; const password = 'your_password'; const basicAuth = 'Basic ' + btoa(username + ':' + password);

axios.get('your_api_url', { headers: { Authorization: basicAuth } }) .then(response => { // 请求成功后的处理逻辑 }) .catch(error => { // 请求失败后的处理逻辑 }); };


注意替换`your_username`和`your_password`为你的用户名和密码,`your_api_url`为你的 API 地址。

4. 在你的组件的 render 方法中,使用 Ant Design 的 Button 组件,并将上面创建的函数作为点击事件处理程序:

```jsx
const YourComponent = () => {
return (
 <Button onClick={handleButtonClick}>发送请求</Button>
);
};

这将在点击按钮时触发handleButtonClick函数,并发送带有 Basic Auth 的请求。

请注意,这只是一个简单的示例,你可能需要根据你的实际需求进行适当的更改和调整。

React Ant Design: 使用 Axios 发送带有 Basic Auth 的请求

原文地址: http://www.cveoy.top/t/topic/l0Ex 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录