Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

XmlHTTPRequest status returns 0

Posted on 2009-06-29
2
Medium Priority
?
3,027 Views
Last Modified: 2013-11-19
I am at a loss for why my code is not working. When I launch the attached html object I get the status code 0 instead of the expected 200. Does anyone know why I am getting this status? I have attached the xml and text files. Change Some.txt to Some.xml
<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.irg/1999/xhtml"><head id="header"><title id="title">This is a test</title><script type="text/javascript">window.onload = initAll;
var xhr = false;
var reqerr = "";
 
function initAll()
{
	document.getElementById("textRequest").onclick = getNewFile;
	document.getElementById("xmlRequest").onclick = getNewFile;
}
 
function getNewFile()
{
	makeRequest(this.href);
	return false; //do not display this url which is a file
}
 
function makeRequest(url)
{
	alert("url is " + url);
	if (window.XMLHttpRequest)
	{
		try
		{
			xhr = new XMLHttpRequest();
		}
		catch (e)
		{
			alert("no xml http request");
		}
	}
	else
	{
		if (window.ActiveXObject)
		{
			try
			{
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					xhr = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e)
				{
					alert("No msxml2");
				}
			}
		}
	}
 
	if (xhr)
	{
		xhr.onreadystatechange = showContents;
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else
	{
		document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpReqest";
	}
}
 
function showContents()
{
	if (xhr.readyState == 4)
	{
		if (xhr.status == 200)
		{
			if (xhr.responseXML && xhr.responseXML.contentType=="text/xml")
			{
				var outMsg = xhr.responseXML.getElementsByTagName("choices")[0].textContent;
			}
			else
			{
				var outMsg = xhr.responseText;
			}
		}
		else
		{
			var outMsg = "There was a problem with the request " + xhr.status + ": " + xhr.statusText;
		}
		document.getElementById("updateArea").innerHTML = outMsg;
	}
}
</script></head><body id="public"><h1 id="body.h1">This is the body</h1><p><a id="textRequest" href="sometext.txt">Reqeust a text file</a><br/><a id="xmlRequest" href="Some.xml">Request an xml file</a></p><div id="updateArea">&amp;nbsp;</div></body></html>

Open in new window

Some.txt
sometext.txt
0
Comment
Question by:atxtechie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 24740694
What is the value of readyState when status == 0?
0
 
LVL 5

Accepted Solution

by:
Opalelement earned 375 total points
ID: 24760906
The status will be 0 until a request is sent. After that the status code will be 200. Since it is the readystatechange event, it should be 200... However if it takes a second it may not get there.

What you can do is replace:

if (xhr.status == 200)

with:

if (xhr.status == 200 || xhr.status == 0)

Once it changes it shouldn't be possible for it to go back to 0, so it will still be okay for either one to be valid.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question