2016-09-26 75 views
-1

嘿,我是新的這個json語法,我有一個問題。 我做了功能從遠程服務器以JSON中獲取數據是這樣的:Json對象在html中顯示

<!DOCTYPE html> 
<html> 
<head> 

    <script type="text/javascript" src="jquery-3.1.0.min.js"></script> 
    <script type="text/javascript"> 

    function getStates(value) { 

      var data = { 
       q: value 
      } 

      $.ajax({ 
       url: 'https://something.hr/api/search', 
       method: 'GET', 
       headers: {'Accept': 'application/json'}, 
       data: data 
      }).done(function(data) { 
       //do something with data I returned to you 
       console.log("success") 
       console.log(data) 


      }).fail(function(data) { 
       //doSomethingWith the data I returned to you 
       console.log("fail") 
       console.log(data) 
      }); 
     }; 



    </script> 
</head> 
<body> 
    <input type="text" onkeyup="getStates(this.value)" > 
    <br> 

<script> 
</script> 

</body> 
</html> 

我的問題是,我知道如何在控制檯日誌對象,但我希望得到該對象在HTML中,就像在一些箱比點擊它,打開他的數據(姓名,身份證,ADRESS等)

+0

JSON.stringify(數據,空,4)將您的JSON返回到一個不錯的字符串偏移4.然後你可以插入你選擇的元素。 –

+0

你可以提供你的JSON樣本嗎? –

+0

https://jsfiddle.net/pfbd8ke6/這是我的結果在控制檯日誌中,首先我得到的對象,當我打開它,我得到那裏inromations。所以現在我希望iformatios排序好 –

回答

0

var httpManager = (function() { 
 
    var getData = function(value) { 
 
    $.ajax({ 
 
     url: 'https://api.github.com/users/zixxtrth', 
 
     method: 'GET' 
 

 
    }).done(function(data) { 
 
     //do something with data I returned to you 
 
     jQuery('#avatar').attr('src', data.avatar_url); 
 
     jQuery('#name').html(data.name); 
 
     jQuery('#username').html(data.login); 
 

 
    }).fail(function(data) { 
 
     //doSomethingWith the data I returned to you 
 
     alert('No Data'); 
 
    }); 
 
    }; 
 

 
    return { 
 
    getData: getData() 
 
    } 
 

 
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> 
 
<img src='' id='avatar' width='200' height='200' /> 
 
<h1 id='name'></h1> 
 
<h2 id='username'></h2>