2016-12-09 8 views
0
var name; 
var what; 
var sol; 
var i; 
var biasData; 
var http = new XMLHttpRequest(); 
http.open("GET","biases.json", true); 
http.onreadystatechange = function() { 
    if(http.readyState === 4) { 
    if(http.status >= 200 && http.status <= 400) { 
     biasData = JSON.parse(http.responseText); 
     console.log(biasData); 
     var data = biasData.slice(0,1); 
     console.log(data); 
     // var data = biasData.splice(0,1); 
     // console.log(data); 
     // i = 0; 
     // var length = biasData.length; 
     // setInterval(function() { 
     // name = biasData[i]["name"]; 
     // what = biasData[i]["explanation"]; 
     // sol = biasData[i]["solution"]; 
     // i++; 
     // if(i >= length) { 
     //  i = 0; 
     // } 
     // }, 10000); 
     var templateScript = document.getElementById('bias').innerHTML; 
     var theTemplate = Handlebars.compile(templateScript); 
     var compiledTemp = theTemplate(data); 
     document.getElementById('main').innerHTML = compiledTemp; 

    } 
    } 
}; 
http.send(); 

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <title>Learning Handlebars.js</title> 
    </head> 
    <body> 
    <div id="main"> 

    </div> 
    <script id="bias" type="x-handlebars-template"> 
     <h1 id="name">{{name}}</h1> 
     <p id="what">{{explanation}}</p> 
     <p id="sol">{{solution}}</p> 
    </script> 
    <a href="#"><button type="button" name="button">Read more at Wikipedia</button></a> 
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.min.js"></script> 
    <script type="text/javascript" src="handle.js"></script> 
    </body> 
</html> 

Der Code hängt keine Werte an meine Seite an. Ich bekomme auch keinen Fehler.Die Handlebars.js fügt meiner Seite keine Werte hinzu

Das ist mein json Daten

[{ 
    "name": "Survivorship Bias", 
    "explanation": "It causes you to overestimate your chances of success. For example, you found a startup and think of it as the next Google, but you don't realise the fact that behind every successful startup there are way many company that don't even make it off the starting line, and out of those who take off, a lot many go bankrupt within first three years.", 
    "solution": "This doesn't mean that you don't even try, but neglecting the correct odds can cause to come up with a terrible plan. A better way to deal with this is to be aware of people's failures too. Take note of the reality, and plan accordingly. " 
}, 
{ 
    "name": "Swimmer's Body Illusion", 
    "explanation": "Have you ever watched a commercial of a cosmetic, in which your favorite actress is endorsing that cosmetic? You think that you should buy that as it will make you look like your favorite actress. The problem is, cosmetic doesn't make your actress look the way she does. The actress is born attractive, and hence, is the chosen candidate for that advertisement.", 
    "solution": "Be honest about yourself. It's not that if you got an MBA, then you'd earn all the money in the world. It's only if you have the best skills, then you'd earn all the money in the world." 
}, 
{ 
    "name": "Clustering Illusion", 
    "explanation": "'Look! The cloud looks like a tortoise.' Haven't you said that before? You may have heard viral stories of people, who observed the face of one of the many Gods on a potato. It is normal. Human brain loves to seek patterns. The worst part is, if your brain can't find a obvious patter, then it will make one.", 
    "solution": "Don't neglect the possibility of something happening by chance. Not everything happens for a reason, sometimes it is just our brain that is messing with us." 
}, 
{ 
    "name": "Social Proof", 
    "explanation": "How often do you do things just because others are doing it too? Let's understand, why we do it. Back in the days of being a cavemen, it was a good survival strategy. When we knew less about our world, it was better to run for our lives, when others were too.", 
    "solution": "Don't buy something just because everybody is buying it. Look for whether or not if buying it can help you with your situation. " 
} 
] 

ich folgende Javascript habe ist Tutorial sexy und zwei Youtube-Videos, aber niemand diese Situation erwähnt.

Jede Hilfe wäre großartig.

EDIT: Ich habe weiter getestet, es ist die JSON-Daten abgerufen, die das Problem verursacht. Obwohl, data wird in der Konsole eingeloggt, aber irgendwie kann die Lenkerfunktion es nicht lesen.

+0

Wie sieht Ihre Vorlage aus? – 76484

+0

Oh, ich habe es jetzt herausgefunden. –

Antwort

0

Okay. Ich habe die Lösung herausgefunden. Wenn Sie jemand sind, der sich in einer ähnlichen Situation befindet. Sehen Sie sich dann die Daten an, die Sie an die Vorlage übergeben.

In meinem Fall data ist ein Array. Also musste ich data[0] verwenden, um auf das Objekt zuzugreifen.

Verwandte Themen