javascript - React 是如何在不需要 jQuery 的情况下允许 $.ajax 的?

标签 javascript jquery ajax reactjs

在 React 教程中,我正在查看 example.js文件

文件在此函数中从服务器获取注释:

var CommentBox = React.createClass({
  loadCommentsFromServer: 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)
    });
  },
// Rest of CommentBox...

但是,代码中没有require('jquery')。 React 没有将 jQuery 列为依赖项。当我在 react 存储库中搜索 ajax 时,没有该函数的任何声明。

这个 $.ajax 函数是 React 特定的函数吗?它是不是 jQuery 以外的东西?感谢您帮助我们更好地理解 React 的这一部分是如何工作的!

最佳答案

React 不需要 需要 jQuery 才能工作。如果你看this page from tutorial website (向上滚动一点)明确提到包含 jQuery 只是为了简化。

这里也包括注释

Note: We included jQuery here because we want to simplify the code of our future ajax calls, but it's NOT mandatory for React to work.

还有 check out the complete section on getting started具有包含 jQuery 的主索引 html 页面。

希望对你有帮助

关于javascript - React 是如何在不需要 jQuery 的情况下允许 $.ajax 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31149079/

相关文章:

javascript - Node.js 无法读取未定义的属性 'on'

javascript - 使用 jquery 在对话框中移动文本字段值

javascript - 如何在加载页面时同步网站内容

c# - MVC4 表单验证 - 自定义 html 消息 howto?

ajax - 如何在将 MVC 与 AJAX 结合使用时改进 SEO

javascript - 检查表单输入字段是否没有改变

javascript - Rails 将 json 传递给 javascript 文件,但 javascript 显示错误

javascript - 当异步为真时,Ajax 调用返回未定义

javascript - 使用 Javascript 捕获 HTTP 请求

javascript - AJAX (JQUERY) 的 Server.Map 路径方法