Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. function cb(data){
  2. for (var i = 0; i < data.length; i++){
  3. console.log(data[i]);
  4. var textEl = document.createElement("div");
  5. textEl.className = "yakker-message";
  6. textEl.innerText = data[i].post;
  7. var timeEl = document.createElement("time");
  8. timeEl.innerText = data[i].date.substring(0,10);
  9. var listItem = document.createElement("li");
  10. listItem.appendChild(textEl);
  11. listItem.appendChild(timeEl);
  12.  
  13. // append el to the container
  14. listContainerEl.appendChild(listItem);
  15. }
  16. }
  17.  
  18. function getPosts(cb){
  19. var request = new XMLHttpRequest();
  20. request.open('GET', '/posts', true);
  21.  
  22.  
  23. request.onload = function() {
  24. if (request.status >= 200 && request.status < 400) {
  25. // Success!
  26. var data = JSON.parse(request.responseText);
  27. cb(data);
  28. } else {
  29. console.log("We reached our target server, but it returned an error")
  30. }
  31. };
  32.  
  33.  
  34. request.onerror = function() {
  35. console.log("There was a connection error of some sort");
  36. };
  37.  
  38.  
  39. request.send();
  40. }
  41.  
  42.  
  43. getPosts(cb);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement