Spring Rest API在Reactjs中不起作用

Spring Rest API在Reactjs中不起作用,spring,reactjs,spring-boot,Spring,Reactjs,Spring Boot,我正在尝试使用ReactJS中的restapi。但它显示的是未定义的。 这是我的密码 代码: 在进行AJAX调用时,我们可以使用axios react,即快速链接: 您可以使用ES6版本的=>,而不是代码中的function关键字 下面是一个从RESTAPI获取响应的示例 建造师{ 超级的 此.state={ 数据:[] } } 组件安装{ axios.getURL .thenres=>{ this.setState{data:res.data}; console.logthis.state.

我正在尝试使用ReactJS中的restapi。但它显示的是未定义的。 这是我的密码

代码:


在进行AJAX调用时,我们可以使用axios react,即快速链接:

您可以使用ES6版本的=>,而不是代码中的function关键字

下面是一个从RESTAPI获取响应的示例

建造师{ 超级的 此.state={ 数据:[] } } 组件安装{ axios.getURL .thenres=>{ this.setState{data:res.data}; console.logthis.state.data; } .catcherror=>{ console.logerror; }; }
在进行AJAX调用时,我们可以使用axios react,即快速链接:

您可以使用ES6版本的=>,而不是代码中的function关键字

下面是一个从RESTAPI获取响应的示例

建造师{ 超级的 此.state={ 数据:[] } } 组件安装{ axios.getURL .thenres=>{ this.setState{data:res.data}; console.logthis.state.data; } .catcherror=>{ console.logerror; }; }
<script type="text/jsx">
            var JavaEEWSTest = React.createClass({
                getInitialState: function () {
                    return {text: ''};
                },
                componentDidMount: function(){

                     $.ajax({
                           url: "http://localhost:8080/hi"
                           }).then(function(data) {

                           this.setState({text: data.text});
                           alert(data.text);
                    }.bind(this))
                 },
                 render: function() {
                     return <div>Response - {this.state.text}</div>;
                 }
             });

            React.render(<JavaEEWSTest />, document.getElementById('component'));

        </script>
@RestController
@CrossOrigin(origins = "http://localhost:3000")
public class HelloController {

    @RequestMapping(value="/hi",method=RequestMethod.GET)
    public String sayHello()
    {
        return "hello";
    }   
}