0

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?

2
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by rileyganimator
1

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

0

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.

0

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?

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.