javascript - How do I get JSON and push it into HTML with jQuery? -


i have html pages , have decided populate them using json generated api (api.example.com).

the thing have never used jquery. basics pull information using jquery , fill html json data, name, surname, messages?

should put javascript codes athe end of html pages (right before )?

or, there easy way?

really depends on json looks likes comming back.

first data using ajax.

var incomingdata; //incoming data variable  $.ajax("http://yoururl.goes.here", {   type: "get",   datatype: "json",   success: function(data) {     incomingdata = data;   },   error: function(req, status, err) {     //console.error("something went wrong! status: %s (%s)", status, err);   } }); 

now have data in incomingdata variable.

i'd console.log data, can view it, accessing parts done through dot notation:

incomingdata.name; incomingdata.phone; 

usually you'll array of objects when return json, may need loop through.

for(var = 0; < incomingdata.length; i++) {     console.log(incomingdata[i]);     $('.yourclass').append(incomingdata[i].name + '<br />'); } 

the above take value of 'name' property , append <div class="yourclass"></div> in html. console log object iterating through in console should able see of properties. confusing @ first, necessary js skill. :)

as far put code, best practices before closing body tag. make sure code running after jquery has been loaded. make main.js file , include right before body closing tag:

<body>     ...     of html     ...     <script src="jquery.js"></script>     <script src="yourfile.js"></script> </body> 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -