在React JS的文本字段中打印JSON数据

时间:2015-12-16 07:23:23

标签: javascript json reactjs

我在componentDidMount:

之后编写的这段代码
componentDidMount: function() {
                  var url = document.URL;  
                  var currentId = url.substring(url.lastIndexOf('?') + 4);//getting the current ID from the url
                  var data = {
                  idtoUpdate: currentId
                  };
                  $.ajax({
                    type: 'POST',
                    url: 'test.php',
                    data: data,
                   success: function(response) {
                    var result = $.parseJSON(response);
                    $("#briefinfo").html(response);//This response will prints the JSON format data which is return by test.php

                   }
                    })
                    .done(function(data) {

                    })
                    .fail(function(jqXhr) {
                    console.log('failed to register');
                    });
                },

这里我得到了JSON格式代码当我返回它打印的响应时 所以我想在文本字段中打印这些数据

1 个答案:

答案 0 :(得分:2)

以下是示例代码......

var someComponent = React.createClass({
    getInitialState:function(){
      return {"response":null};
    },
    componentDidMount: function() {
      var url = document.URL;  
      var currentId = url.substring(url.lastIndexOf('?') + 4);//getting the current ID from the url
      var data = {
        idtoUpdate: currentId
      };
      $.ajax({
        type: 'POST',
        url: 'test.php',
        data: data,
       success: function(response) {
        var result = $.parseJSON(response);
        this.setState({"response": response});
       }.bind(this)
      })
      .done(function(data) {

      })
      .fail(function(jqXhr) {
        console.log('failed to register');
      });
    },
    render: function(){
      return (
        <textarea value={this.state.response} readonly={true}/>
      );
    }
});