2015-12-16 31 views
0

這個代碼我componentDidMount後寫:打印文本字段的JSON數據作出反應JS

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格式代碼,當我回到它打印響應 所以我要打印這個數據在文本字段

+0

我建議存儲狀態和渲染方法中的響應寫狀態值作爲輸入/特克斯的值tarea標籤。 – hazardous

回答

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

這不是你想要的嗎?請用一個示例來說明文本區域應顯示的內容/內容。 – hazardous

+0

感謝@FakeRainBrigand進行重要編輯。 – hazardous

+0

文本區域只顯示特定ID的名稱而不是JSON數據 – Param