Confused about Ajax, Basic XMLHTTPRequest
- by George
I'm confused about the basics of Ajax. Right now I'm just trying to build a basic Ajax request using plain JavaScript to better understand how things work (as opposed to using Jquery or another library).
First off, do you always need to pass a parameter or can you just retrieve data? In its most basic form, could I have an html document (located on the same server) that just has plain text, and another html document retrieve that text and load it on to the page?
So I have fox.html with just text that says "The quick brown fox jumped over the lazy dog." and I want to pull in that text into ajax.html on load. I have the following on ajax.html
<script type="text/javascript">
function createAJAX() {
var ajax = new XMLHttpRequest();
ajax.open('get','fox.html',true);
ajax.send(null);
ajax = ajax.responseText;
return(ajax);
}
document.write(createAJAX());
</script>
This currently writes nothing when I load the page.