2016-09-26 2 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 그것을 클릭하고 열보다 (데이터를 널 (null), 4) 좋은 문자열에에 JSON을 반환합니다 offset 4. 원하는 요소에 삽입 할 수 있습니다. –

+0

JSON 샘플을 제공 할 수 있습니까? –

+0

https://jsfiddle.net/pfbd8ke6/ 이것은 콘솔 로그에서 내 결과입니다, 먼저 객체를 얻습니다. 내가 열면 inrop가 나타납니다. 그래서 이제는 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>