Message from JavaScript discussions
June 2017
— Ex :
<button id="q">page 1</button>
<div id="page"></div>
<script>
Document.getElementById("q").addEventListener("click", function(){
var a = new XMLHttpRequest();
a.open("GET","/page1.json",true);
a.onload = function(){
var q = JSON.parse(this.responseText);
document.getElementById("page").innerHTML += q['page'];
};
a.send(null);
});
</script>
— Json : {"page": "hello world"}
— Can u tell me about this
var q = JSON.parse(this.responseText);
document.getElementById("page").innerHTML += q['page'];
?
— Wait, i will test it first
— Https://www.crayner.cf/test/test.html
— Https://www.crayner.cf/test/page1.json
— You can use innerHTML for change the HTML content
— JSON.parse()
— A common use of JSON is to exchange data to/from a web server.
— When receiving data from a web server, the data is always a string.
— Parse the data with JSON.parse(), and the data becomes a JavaScript object.