首页 > 解决方案 > Axios 向我抛出“未经授权”

问题描述

这很奇怪,因为邮递员允许我进行 POST,但 react 不允许。令牌是有效的,当被调用时,令牌会打印到控制台

export default class Create extends Component {
    //url = 'http://127.0.0.1:5000/wtw/articles/'
    constructor(props) {
        super(props);
        this.state = {
            //author?
            title: '',
            description: '',
            //image: ,
            //token: null
        };
    }

    handleCreate = async () => {
        const { title, description, image } = this.state;

        let token = await AsyncStorage.getItem('token');

        axios
            .post('http://127.0.0.1:5000/wtw/articles/', {
                headers: {
                    Authorization: 'Token ' + token,
                },
                data: {
                    title: title,
                    description: description,
                },
            })
            .then(async (Response) => {
                console.log(Response);
            })
            .catch((err) => {
                console.log();
                console.log(err);
                console.log(token);
            });
    };
}

标签: react-nativeauthenticationaxiosauthorization

解决方案


推荐阅读