Param
Param

Reputation: 21

Print the JSON data in a text field in React JS

this code I am writing after 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');
                    });
                },

Here I am getting the JSON format code When I return the response it prints so I want to print this data in a textfield

Upvotes: 0

Views: 2752

Answers (1)

hazardous
hazardous

Reputation: 10837

Here's a sample code...

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

Upvotes: 2

Related Questions