Handling ajax with React

前端 未结 5 1050
南笙
南笙 2020-12-02 13:11

How should I handle ajax requests in a fairly traditional web application? Specifically with using React for views, while having a backend that handles data such as text and

5条回答
  •  天命终不由人
    2020-12-02 13:48

    Just in case anybody stumbled upon this and does not know, jQuery makes it super easy to send AJAX calls. Since React is just JavaScript it will work just like any other jQuery AJAX call.

    React's own documentation uses jQuery to make the AJAX call so I assume that's good enough for most purposes regardless or stack.

    componentDidMount: function() {
        $.ajax({
          url: this.props.url,
          dataType: 'json',
          cache: false,
          success: function(data) {
            this.setState({data: data});
          }.bind(this),
          error: function(xhr, status, err) {
            console.error(this.props.url, status, err.toString());
          }.bind(this)
        });
      },
    

提交回复
热议问题