I am making a ajax call my my API which returns some JSON. I would like like to use certain values from the JSON to dynamically update my webpage.
<div class="Name"></div>
<div class="Role"></div>
<div class="Location"></div>
<div class="Team"></div>
I know I have my response data here success: function(data) but I am unsure on how to call the values I want and then put them into variables I can use on my page.
For example on my page <div class="Name"></div> should show john smithin plain html.
HTML With Ajax Call
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>1</title>
<style>
img {
height: 100px;
float: left;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div class="Name"></div>
<div class="Role"></div>
<div class="Location"></div>
<div class="Team"></div>
<script>
var authorizationToken = "xxxxxxxxxxxxxxxxxxx";
function myapiRequest(endpoint, method, options) {
$.ajax($.extend({}, {
type: 'GET',
dataType: "json",
success: function(data) { // my successfully reutnred json
},
url: "https://api.myapi.com/" + endpoint,
headers: {
"Authorization": "Token token=" + authorizationToken,
"Accept": "application/vnd.myapi+json;version=2"
}
},
options));
}
myapiRequest('/users/12345G?include%5B%5D=contact_methods&include%5B%5D=teams'); // this will be a variable
</script>
</body>
</html>
Response
{
"user":{
"name":"john smith",
"email":"[email protected]",
"time_zone":"Asia/Hong Kong",
"color":"crimson",
"billed":true,
"role":"user",
"description":null,
"invitation_sent":false,
"contact_methods":[
{
"id":"55666655",
"type":"email_contact_method",
"summary":"Work",
"html_url":null,
"label":"Work",
"address":"[email protected]",
"send_short_email":false,
"send_html_email":true
},
{
"id":"55554555",
"type":"email_contact_method",
"summary":"Work",
"html_url":null,
"label":"Work",
"address":"[email protected]",
"send_short_email":false,
"send_html_email":true
},
{
"id":"5455555",
"type":"email_contact_method",
"summary":"Work",
"html_url":null,
"label":"Work",
"address":"[email protected]",
"send_short_email":false,
"send_html_email":true
},
{
"id":"5444555",
"type":"email_contact_method",
"summary":"Work",
"html_url":null,
"label":"Work",
"address":"[email protected]",
"send_short_email":false,
"send_html_email":true
}
],
"teams":[
{
"id":"232656TM",
"name":"team1",
"description":null,
"type":"team",
"summary":"team1 support",
"privilege":null
},
{
"id":"25656TM",
"name":"team1",
"description":null,
"type":"team",
"summary":"team1 support",
"privilege":null
},
{
"id":"P767676TM",
"name":"team1tt",
"description":null,
"type":"team",
"summary":"team1 support",
"privilege":null
}
],
}
}
**around it? That's not valid JSON."user". To access that, you can dodata.user. That will return the object that is the value of user. If you just wanted the user name, you could dodata.user.name. If you wanted something from a single contact method, you would need to loop like this$(data.user.contact_methods).each(function(){});and in that add logic to filter out the unwanted results. Make sure the object returned isn't in JSON string format, other wise you need to callJSON.parse()on whatever is stringified.