gudii9
asked on
ajax json server file
hi,
i was trying to understand below ajax json example using xmlhttpobject to get file data from server
https://www.w3schools.com/js/tryit.asp?filename=tryjson_ajax
i am not sure what is happening here.
what it means by below?
target="_blank
where is the server path?
what is meaning of below lines
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseTe xt);
document.getElementById("d emo").inne rHTML = myObj.name;
}
};
xmlhttp.open("GET", "json_demo.txt", true);
xmlhttp.send();
when is onreadystatechange related function called and what is 4 and 200 here?
why we need to use JSON.parse
and
document.getElementById
and
myObj.name;??
please advise
i was trying to understand below ajax json example using xmlhttpobject to get file data from server
https://www.w3schools.com/js/tryit.asp?filename=tryjson_ajax
i am not sure what is happening here.
<!DOCTYPE html>
<html>
<body>
<h2>Use the XMLHttpRequest to get the content of a file.</h2>
<p>The content is written in JSON format, and can easily be converted into a JavaScript object.</p>
<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
}
};
xmlhttp.open("GET", "json_demo.txt", true);
xmlhttp.send();
</script>
<p>Take a look at <a href="json_demo.txt" target="_blank">json_demo.txt</a></p>
</body>
</html>
what it means by below?
target="_blank
where is the server path?
what is meaning of below lines
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseTe
document.getElementById("d
}
};
xmlhttp.open("GET", "json_demo.txt", true);
xmlhttp.send();
when is onreadystatechange related function called and what is 4 and 200 here?
why we need to use JSON.parse
and
document.getElementById
and
myObj.name;??
please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Open in new window
if no server how it is fetching the file.what is overall flow of this example. how ajax related to this
it simply displays json in next tab?
please advise