【问题标题】:Reactjs problems when ajax data from json file来自json文件的ajax数据时的Reactjs问题
【发布时间】:2017-11-30 22:07:03
【问题描述】:

我在开发 React Web 应用程序时遇到问题。这是我的代码:

class TableContentRow extends React.Component {
        render(){
            return(
                    <tr>
                        <td>{this.props.voucher.merchantName}</td>
                        <td>{this.props.voucher.voucherCode}</td>
                        <td>{this.props.voucher.orderId}</td>
                        <td>{this.props.voucher.deal}</td>
                        <td>{this.props.voucher.dealDescription}</td>
                        <td>{this.props.voucher.price}</td>
                        <td>{this.props.voucher.redemptionStatus}</td>
                        <td>{this.props.voucher.redemptionTimestamp}</td>
                    </tr>
            );
        }
    }

class TableContent extends React.Component {
    render() {
        const rows = [];
        this.props.vouchers.forEach((voucher) => {
            if(voucher.orderId.indexOf(this.props.filterText) === -1){return;}
            rows.push(<TableContentRow voucher = {voucher} key = {voucher.orderId} />);
        })

        return(
                <div className="panel panel-primary">
                    <div className="panel-heading">
                        <h3 className="panel-title">
                            All Vouchers
                        </h3>
                    </div>

                    <table className="table table-striped">
                        <thead>
                        <tr>
                            <th>Restaurant</th>
                            <th>Voucher Code</th>
                            <th>Order ID</th>
                            <th>Deal</th>
                            <th>Deal Description</th>
                            <th>Sale Price</th>
                            <th>Redemption Status</th>
                            <th>Redemption Timestamp</th>
                        </tr>
                        </thead>
                        <tbody>
                        {rows}
                        </tbody>
                    </table>
                </div>
        );
    }
}

class VoucherAll extends React.Component {
    constructor(props){
        super(props);
        this.handleFilterTextInput = this.handleFilterTextInput.bind(this);
        this.loadVouchersFromServer = this.loadVouchersFromServer.bind(this);
        this.state = {filterText: ''};
    }

    handleFilterTextInput(filterText) {
        this.setState({
            filterText: filterText
        });
    }

    loadVouchersFromServer() {
        $.ajax({
            url: this.props.url,
            success: function(data) {
                this.setState({
                    data: data
                });
            },
            error: function(xhr,status,err) {
                console.log(this.props.url, status, err.toString());
            }
        })
    }

    componentDidMount() {
        this.loadVouchersFromServer();
        setInterval(this.loadVouchersFromServer, this.props.pollInterval);
    }

    render(){
        return(
                <div className="container">                       
                    <TableContent
                            vouchers = {this.state.data}
                            filterText = {this.state.filterText}
                    />                       
                </div>
        );
    }
}

ReactDOM.render(
        <VoucherAll url = "voucher.json" pollInterval = {2000} />,
    document.getElementById('voucherAll')
)

这是我的 json 文件:

{
  "merchantName":"xxxx",
  "voucherCode":"xxxx",
  "orderId":"xxxx",
  "deal":"xxxx",
  "dealDescription":"xxxx",
  "price":"xxxx",
  "redemptionStatus":"xxxx",
  "redemptionTimestamp":"xxxx-xx-xx"
}

当我运行我的代码时,网页什么也没有显示。在控制台中,我找不到任何相关消息。谁能帮我解决这个问题?谢谢。

【问题讨论】:

标签: json ajax reactjs


【解决方案1】:

您在 ajax 回调中丢失了上下文。尽管loadVouchersFromServer 绑定了successerror 回调没有。您可以使用箭头函数或bind 那些回调。

loadVouchersFromServer() {
    $.ajax({
        url: this.props.url,
        success: data => {
            this.setState({
                data: data
            });
        },
        error: function(xhr,status,err) {
            console.log(this.props.url, status, err.toString());
        }.bind(this)
    })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-04
    • 2018-04-13
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    相关资源
    最近更新 更多