发布axios并改变状态

时间:2017-01-04 08:37:32

标签: javascript reactjs post promise axios

我不会将“React”状态(this.state.result)更改为axios post。怎么了?你帮我吗?我使用api进行实时搜索。如何在axios承诺中获得结果?

componentDidMount: function() {
    function Func(a) {return  a }
    const Good = axios.post(
        'https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/party', // url
        { 'query': this.state.names }, // data
        { headers: 
            { 'Content-Type':  'application/json',
            'Accept':        'application/json',
            'Authorization': 'Token ' + 'a7d88db5aee03e2e2c55722ce2c1d66b18da0cff',
            'X-Secret':'5f98a91e857ac197a9a7177f1b66e28c304760cf',
            },
        } 
    )
    .then(function (response,result) {
        let data = JSON.parse( response.request.response );
        this.setState( { result: data} );//It's error. The result is underfined
    })
    .catch(function (error) {
        console.log(error);
    });
}

0 个答案:

没有答案