gpt4 book ai didi

javascript - 为什么我无法在控制台中显示 api 的内容(出现 401 错误)?

转载 作者:行者123 更新时间:2023-12-03 01:01:06 25 4
gpt4 key购买 nike

我想在控制台中显示此 api 的内容,但它不起作用。

我做错了什么以及如何解决它?

我收到的错误:

Error: Request failed with status code 401

这是我的代码:

import React, { Component } from 'react';
import axios from 'axios';

class Flights extends Component {
constructor(props) {
super(props);
}

componentDidMount() {
axios.get('https://jsonplaceholder.typicode.com/posts')
.then(response => {
console.log("API Call ====> " + response);
}).catch(error => {
console.log(error);
})
}

render() {
return(
<div></div>
);
}
}

export default Flights;

最佳答案

您可以在打印之前使用 JSON.stringify() 方法将响应内容解析为字符串。

这是演示:https://codesandbox.io/s/m4nq5lrxxp

另一种方法是直接打印它:

 axios
.get("https://jsonplaceholder.typicode.com/posts")
.then(response => {
console.log(response.data); // like this without string concatenation
})
.catch(error => {
console.log(error);
});

关于javascript - 为什么我无法在控制台中显示 api 的内容(出现 401 错误)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52618529/

25 4 0
文章推荐: javascript - 尽管使用了 db.insert 函数,数据库并未更新
文章推荐: objective-c - 如何使用 Objective C 从特定程序中捕获音频?
文章推荐: powershell - 忽略输出-它不应在屏幕上打印
文章推荐: javascript - ReactJS 和