Trying my hand a Javascript, I came across an issue with XMLHttpRequest. This is probably something I'm doing wrong, so any help would be appreciated.

reader = new XMLHttpRequest()
reader.open("GET","Votes.xml")
alert(reader.readyState)
reader.send()
alert(reader.readyState)

when I run this, it alerts that the readyState is 1 (Send has not been called) and then stops. If I comment out the "reader.send()" then it continue with the second alert (And all the code that follows). Am I missing something simple?

Recommended Answers

All 5 Replies

I'm not quite sure just what you're trying to do, but I've used XMLHttpRequest and so maybe this will help.

var reader = new XMLHttpRequest();
reader.open("GET", hostpath + "Votes.xml", true);
reader.onreadystatechange = function ()
{
    if (reader.readyState == 4)
    {
        if (reader.status == 200)
        {
            var votes = reader.responseText;
        }
    }
}
alert(reader.readyState);
reader.send(null);
alert(reader.readyState);

You would then get the contents of the file into a variable and it should alert the readyState.
Hope this helps...

That helped a little. Using the onreadystatechange the readystate is now 4, but the status remains 0 and the variable doesn't get any of the files contents.

and
hostpath+"Votes.xml"
causes the program to stop, but just useing "Votes.xml" runs fine.

Oh, I don't know why I included hostpath... My bad.
Ummm, I'm not quite sure what might've happened, but perhaps Votes.xml is empty?

Votes.xml

<Poll>
    <Name>Voter 1</Name>  <Vote>5</Vote>
</Poll>

Thought I'd start with a small file and try to read a single vote.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.