Confused about Ajax, Basic XMLHTTPRequest

Posted by George on Stack Overflow See other posts from Stack Overflow or by George
Published on 2010-05-26T15:10:24Z Indexed on 2010/05/26 15:11 UTC
Read the original article Hit count: 368

Filed under:

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.

© Stack Overflow or respective owner

Related posts about AJAX