反应代码
import React, { useState, useEffect } from "react";
import axios from "axios";
const Table = () => {
useEffect(() => {
console.log("helllllllllllllllo");
callAPI();
}, []);
const callAPI = async () => {
const url = "some URL";
const password = "Secret" ;
const username = "Consumer Key";
const data = await axios.get(
url,
{},
{
auth: {
username: username,
password: password,
},
}
);
console.log("data", data);
};
return (
<div> Hello </div>
);
};
export default Table;
postman,我转到
Authorization
选项卡,然后在其各自的输入字段中输入用户名和密码,并获得结果,但是使用Axios,我会遇到错误。
确切的错误是: - 401
createError.js:16 Uncaught (in promise) Error: Request failed with status code 401
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:62)
中,第一个参数为URL,第二个参数为
GET
对象。在config
对象中,您可以提供config
属性以发送basikauth标题。
auth