使用 React 处理 ajax [英] Handling ajax with React

查看:20
本文介绍了使用 React 处理 ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在相当传统的 Web 应用程序中,我应该如何处理 ajax 请求?特别是使用 React 进行视图,同时有一个后端处理文本等数据,但使用 ajax 自动保存用户交互,例如切换选项或喜欢帖子到服务器.

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 what not, but using ajax to automatically save user interactions such as toggling options or liking a post to the server.

我应该为此使用 jQuery,还是像 Backbone 这样的东西更有益?

Should I just use jQuery for this, or would something like Backbone be more beneficial?

推荐答案

以防万一有人偶然发现并且不知道,jQuery 使发送 AJAX 调用变得非常容易.由于 React 只是 JavaScript,它会像任何其他 jQuery AJAX 调用一样工作.

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 自己的文档使用 jQuery 进行 AJAX 调用,所以我认为这对于大多数用途来说已经足够了,无论是堆栈还是堆栈.

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)
    });
  },

这篇关于使用 React 处理 ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆