【问题标题】:Axios Request Failure In ReactReact 中的 Axios 请求失败
【发布时间】:2017-02-26 13:13:04
【问题描述】:

我不确定我在这里做错了什么。

我希望Main 组件将ajax 请求方法传递给子组件InputForm,这将返回将存储在Main 组件状态中的结果。

class Main extends React.Component {
    constructor( props ){
        super( props );
        this.state = {
            fetching : false,
            repos : {}
        };
        this.getGitHubRepo = this.getGitHubRepo.bind( this );
    }
    getGitHubRepo( user ){
        this.setState({ fetching : true });
        console.log( "form submitted!", user );
        axios.get( user )
            .then(( response ) => {
                console.log( "response =>", response );
            })
            .catch(( error ) => {
                console.log( "error => ", error );
            });
    }
    render(){
        return(
            <div className = "appContainer">
                <InputForm getGitHubRepo = { this.getGitHubRepo } />
            </div>
        );
    }
}



class InputForm extends React.Component{
    constructor( props ){
        super( props );
        this.state = {
            inputValue : "",
        };
        this.recordInput = this.recordInput.bind( this );
    }
    recordInput( e ){
        this.setState({ inputValue : e.target.value });
    }
    render(){
        let getPath = `https://api.github.com/${this.state.inputValue}`;
        return(
            <form onSubmit = {() => this.props.getGitHubRepo( getPath )}>
                <label htmlFor = "user_input">
                    Github Username
                </label>
                <input id = "user_input"
                         type = "input"
                         onChange = { this.recordInput } />
                <input type = "submit" value = "get repos" />
            </form>
        );
    }
}


ReactDOM.render( <Main />, document.getElementById( "app" ));

Here is the jsbin link.

我没有得到任何结果,我的 webpack 服务器刷新了页面。

【问题讨论】:

    标签: javascript ajax reactjs axios


    【解决方案1】:

    这里的主要问题是您没有在表单提交时调用 preventDefault。 另外,获取 github repos 的 url 是错误的,但这是次要的。

    查看更新的jsbin:https://jsbin.com/sujakexamo/1/edit?js,output

    class InputForm extends React.Component{
        constructor( props ){
            super( props );
            this.state = {
                inputValue : "",
            };
            this.recordInput = this.recordInput.bind( this );
            this.submit = this.submit.bind(this);
        }
    
        recordInput( e ){
            this.setState({ inputValue : e.target.value });
        }
    
        submit (e) {
          e.preventDefault();
          this.props.getGitHubRepo( `https://api.github.com/users/${this.state.inputValue}/repos` );
        }
    
        render(){
            return(
                <form onSubmit = {this.submit}>
                    <label htmlFor = "user_input">
                        Github Username
                    </label>
                    <input id = "user_input"
                             type = "input"
                             onChange = { this.recordInput } />
                    <input type = "submit" value = "get repos" />
                </form>
            );
        }
    }
    

    【讨论】:

    • 谢谢@Canastro。我没有意识到 preventDefault。
    猜你喜欢
    • 1970-01-01
    • 2020-11-26
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2021-01-12
    • 2021-11-16
    相关资源
    最近更新 更多