如何使用Axios在React

问题描述 投票:0回答:1

反应代码

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

	
javascript reactjs axios postman
1个回答
3
投票

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


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.