0

I'm fairly new to the whole "backend" development.I set up a localhost wrote my html request with no visible syntax errors, but my new content still wont update the page.

<!DOCTYPE html>
<html>
<head>
    <title>Loading Html With AJAX</title>
</head>
<body>
<header><h1>Maker Bus</h1></header>
<section id="content"></section>
<script type="text/javascript" src="/Applications/MAMP/htdocs/loadingHtml.js"></script>
</body>
</html>




var xhr= new XMLHttpRequest();

xhr.onload=function(){
    //Server Check
    if(xhr.status==200){
        document.getElementById('content').innerHTML=xhr.responseText;

    }
};

xhr.open('GET' ,'http://localhost:8888/data.html',true);
xhr.send(null);
Kishore Indraganti
  • 1,296
  • 3
  • 17
  • 34

2 Answers2

1

There are alot of things missing in this ajax request...including that you are using onload function which might be supported by your browser.Look at this link

1.You are not making a browser independent XMLHttpRequest like for IE we would need an ActiveXObject ...

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

2.You should use the onreadystatechange function(which gets called when the response is ready) what you used wont work as the response wont be ready at that time.

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }

3.The url to call to get the response from

xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();

For the full example read here

Community
  • 1
  • 1
cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
0

simply type this function in your javacript files, am sure it will work for you.

function getContent(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var content = document.getElementById("content");
content.value  = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "data.html", true);
xmlhttp.send();
}

//ON YOUR BODY TAGS IN HTML PAGE 
<body onload="getContent()"></body>

This will absolutely work trust me i have been in the same situation for the whole week; please mark if you succeed.