문제

I'm trying to display the values of a JSON, coming from a LinkedIn API specifically IN.API.Profile(), I am able to get to output the values by using "alert(JSON.stringify(result));", however I'm trying to put the api values to their corresponding places, (firstName = firstName label etc...)

When the button is clicked the firstName should display in the 1st label and so on and so forth.
How can I accomplish this?

Code:

<html>
  <head>
    <script type="text/javascript" src="http://platform.linkedin.com/in.js">
  api_key: XXXXXXXXXXX
  authorize: true
  onLoad: getProfileData
</script>

<script>
   function getProfileData() {
    IN.API.Profile("me")
      .fields(["firstName", "lastName", "headline", "summary"])
      .result(function(result) {
     alert(JSON.stringify(result));
     //document.write(me.fields.firstName).value = firstName;
      });
  }

  </script>

  </head>
  <body>  

  <input  id="firstName">
  <input id="lastName">
  <input id="headline">
  <input id="summary">
  <button id="Add" onclick="getProfileData()">Add Boss</button>
  </body>
</html>

Update...........................................................

Sir by element you mean (firstName, lastName, etc...)? this what I did sir but it promps an error that says getProfileData undefined

function getProfileData() {
     .fields(["firstName", "lastName", "headline", "summary"])
     .result(function (result) {
    for (var fields in fields(result)) {
        document.getElementById("firstName").value = result[0];
        document.getElementById("lastName").value = result[1];
        document.getElementById("headline").value = result[2];
        document.getElementById("summary").value = result[3];  
    }
})
}
도움이 되었습니까?

해결책

The .result() passes a JavaScript array to the function gave, no need to parse or stringify it as JSON. If you want to set each field's value from that function, you'd need something like this:

.result(function (result) {
    for (var key in result) {
        document.getElementById(key).value = result[key];
    }
})
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top