Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function cb(data){
- for (var i = 0; i < data.length; i++){
- console.log(data[i]);
- var textEl = document.createElement("div");
- textEl.className = "yakker-message";
- textEl.innerText = data[i].post;
- var timeEl = document.createElement("time");
- timeEl.innerText = data[i].date.substring(0,10);
- var listItem = document.createElement("li");
- listItem.appendChild(textEl);
- listItem.appendChild(timeEl);
- // append el to the container
- listContainerEl.appendChild(listItem);
- }
- }
- function getPosts(cb){
- var request = new XMLHttpRequest();
- request.open('GET', '/posts', true);
- request.onload = function() {
- if (request.status >= 200 && request.status < 400) {
- // Success!
- var data = JSON.parse(request.responseText);
- cb(data);
- } else {
- console.log("We reached our target server, but it returned an error")
- }
- };
- request.onerror = function() {
- console.log("There was a connection error of some sort");
- };
- request.send();
- }
- getPosts(cb);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement