mardi 31 décembre 2019

How to run this AJAX JAVA SCRIPT Coding?

My Source from W3 Schools : https://www.w3schools.com/js/tryit.asp?filename=tryjson_ajax_array

When I running this ajax example in w3 source it's running successfully.

When I try to execute the same code in my local its' not running .The code is below.

Is there I need any server for running this example.

ajax.html

<!DOCTYPE html>
<html>
<body>

<h2>Use the XMLHttpRequest to get the content of a file.</h2>

<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>

</body>
</html>

json_demo.txt

{
    "name":"John",
    "age":31,
    "pets":[
        { "animal":"dog", "name":"Fido" },
        { "animal":"cat", "name":"Felix" },
        { "animal":"hamster", "name":"Lightning" }
    ]
}

How to run this file in my local computer? Help me to begin my Ajax Learning......




Aucun commentaire:

Enregistrer un commentaire