react.js - 为什么我无法发送Ajax请求??

查看:141
本文介绍了react.js - 为什么我无法发送Ajax请求??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

   var App = React.createClass({
        render : function () {
            return (
                    <div className="col-sm-8 col-sm-offset-2" style={{marginTop:"50px"}}>
                        <Head1 />
                        <table className="table">
                            <Head2 />
                            <Body />
                        </table>
                    </div>
            )
        }
    })
    var Head1 = React.createClass({
        render : function () {
            return (
                        <p className="text-center">Leaderboard</p>
            )
        }
    })
    var Head2 = React.createClass({
        render : function () {
            return (
                    <thead style={{backgroundColr:"#FFFFFF"}}>
                    <tr>
                        <th>#</th>
                        <th>Camper Name</th>
                        <th>Points in past 30 days</th>
                        <th>All time points </th>
                    </tr>
                    </thead>
            )
        }
    })
    var Body = React.createClass({
        getInitialState : function () {
          return {
              value : ["!"]
          }
        },
        getData : function () {
          $.get("http://fcctop100.herokuapp.com/api/fccusers/top/recent",function (msg) {
             // console.log("!!!!!!!!!")
              console.log(msg)
          })
        },
        componentDidMount : function () {

            this.setState({
                value : this.getData
            })

        },
        render : function () {
            console.log(this.state.value)
            return (
                    <tbody>
                    <tr>
                        <th>1</th>
                        <th>dasdas</th>
                        <th>45</th>
                        <th>454</th>
                    </tr>
                    </tbody>
            )
        }
    })
    React.render(<App/>,document.getElementById("container"))

我在Body组件里面想使用Ajax请求获得数据并且渲染,但是现在连请求都不成功。。。
不知道是哪里出了问题。。

这个问题已被关闭,原因:问题已解决 - 问题已解决,且对他人无借鉴意义

解决方案

getData是个异步的方法,你这么写只是给把这个方法给value赋值了,并没有执行
应该在componentDidMount里触发getData,然后在getData的异步请求的回调里setState

这篇关于react.js - 为什么我无法发送Ajax请求??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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