Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么可以';我是否在控制台中显示api的内容(获取401错误)?_Javascript_Reactjs_Debugging_Axios_Http Status Code 401 - Fatal编程技术网

Javascript 为什么可以';我是否在控制台中显示api的内容(获取401错误)?

Javascript 为什么可以';我是否在控制台中显示api的内容(获取401错误)?,javascript,reactjs,debugging,axios,http-status-code-401,Javascript,Reactjs,Debugging,Axios,Http Status Code 401,我想在控制台中显示此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);

我想在控制台中显示此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;
import React,{Component}来自'React';
从“axios”导入axios;
类扩展组件{
建造师(道具){
超级(道具);
}
componentDidMount(){
axios.get()https://jsonplaceholder.typicode.com/posts')
。然后(响应=>{
log(“API调用==>”+响应);
}).catch(错误=>{
console.log(错误);
})
}
render(){
返回(
);
}
}
导出默认航班;

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

这是演示:

另一种方法是直接打印:

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

{“错误”:{“状态”:401,“消息”:“未提供令牌”}}@sjdm这是什么意思?我猜需要某种访问令牌或身份验证令牌才能使GET request401未经授权-请求需要用户身份验证,或者,如果请求包含授权凭据,已拒绝这些凭据的授权。这是来自SPOTIFY DOCSoh的,所以问题不在于我的代码,而是API太安全了,不允许任何人访问它。对吗?对!这不是你的代码,这是关于API的,你可以按照他们的文档来做。详情如下:查看我的原始帖子,我将其更改为
https://jsonplaceholder.typicode.com/posts 
这次在控制台中,我看到了
[object object]
(它没有遇到错误)。为什么我不能显示它的内容?可以使用JSON.stringify()将响应内容解析为字符串。这是演示:
响应。数据
在整个控制台重复打印
[对象]
JSON.stringify()
打印内容,但内容非常混乱。