jquery - Javascript array elements not being displayed in div tag -
i've created program reads json data concert event. json file has 1 object named global has band name , venue. there tickets object containing of available tickets concert, getting ticket price, section, , row (for seats). when using console.log()
print out specific attributes parsed json, getting correct output. have function reads parsedjson object different arrays (one ticketinfo , general event info). in function, use jquery functionality add contents of arrays div on page, nothing being displayed when page loads. i'm new jquery have simple mistake causing problem can tell code wrong?
the relevant code below:
<div id="container"> container div </div> <script> var concertdata = {}; var eventinfo = {}; var ticketinfo = {}; function makeinvite() { var metainfo = concertdata['global'][0]; eventinfo['venue'] = metainfo['maptitle']; eventinfo['band'] = metainfo['productionname']; (var = 0; < concertdata['ticket'].length; i++) { var ii = concertdata['ticket'][i]; var temp = { 'section': ii['l'], 'price': ii['p'], 'row': ii['r'], }; ticketinfo[i] = temp; } } function buildquestiontoscreen() { var inviteobj = $('<div style="margin:20px"></div>'); inviteobj.append( '<div>invite friend see ' + eventinfo['band'] + '?</div>' ); var $div = $("<div></div>"); var $divline = $("<tr></tr>"); console.log(eventinfo['band']); var $table = $("<table></table>"); (var j = 0; j < ticketinfo.length; j++) { var $line = $("<tr></tr>"); $line.append($("<td></td>").html(ticketinfo[j][0])); $line.append($("<td></td>").html(ticketinfo[j][1])); $line.append($("<td></td>").html(ticketinfo[j][2])); $table.append($line); } //$table.appendto(document.body); $inviteobj.appendto($("#container")); $table.appendto($("#container")); } $.ajax({ url: 'concertinfo.json', success: function(data){ //console.log(data); concertdata = data; makeinvite(); buildquestiontoscreen(); $(data.tickets).each(function(index, value){ console.log(value.p); }); } }) </script>
edit-here relevant part of json file being read from:
{ "global": [ { "dte": "1", "atp": "116.33", "lp": "74.00", "hp": "183.00", "listingcount": "3", "hq": "8", "vpcr": "exp0818", "maptitle": "terminal 5", "productionid": "1817728", "productionname": "glass animals", "eventid": "35873", "venueid": "5351", "zoned": "0", "staticmapurl": "http://d2o50i5c2dr30a.cloudfront.net/e19c6a1e-f606-46df-82c2-230544edc2a5.jpg", } ], "tickets": [ { "s": "general a..", "r": "ga6", "q": "1", "p": "74.00", "i": "vb916157659", "l": "general admission", },
i suppose problem assigning concertdata
variable returned string data
, not parsed json in ajax call.
try changing line following line
concertdata = data;
to
concertdata = json.parse(data);
alternatively, instead of above tweak, can specify datatype
property (set 'json'
) ajax call. in case, "json" evaluates response of request json , returns javascript object.
$.ajax({ url: 'concertinfo.json', datatype: 'json', success: function(data){ concertdata = data; makeinvite(); buildquestiontoscreen(); } })
Comments
Post a Comment