Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XMLHttpRequest in IE and Chrome

Status
Not open for further replies.

GomezAddamz

Technical User
Jan 23, 2008
26
0
0
US
Hiya! I wrote a simple JavaScript to parse a CSV file into an HTML table. Works great in FF (23), not so much in IE (8) or Chrome (35). It seems like the XMLHttpRequest is gumming up the works. IE will create the request, but doesn't seem to want to open it. Chrome will open it, but chokes when it tries to send. I've hit the web, and it seems like the code should be the same for each browser, so I'm not sure what I'm doing wrong. I'd appreciate it if you could take a look and hopefully point me in the right direction. Thanks!

JavaScript:
<html>
<head>
<title>
Supplies
</title>

<style>
table, td {
	text-align: center;
	border-collapse: collapse;
	border: 1px solid black;
}
</style>

<script type="text/javascript">

function initialize() {
	request = new XMLHttpRequest();
alert("Request!");
	request.open('GET','Supplies.csv',false);
alert("Open!"); //IE fails here
	request.send();
alert("Sent!"); //Chrome fails here
	rows = request.responseText.split("\n");

	for (i = 0; i < rows.length; i++) {
		trow = "<tr>";
		tdata = rows[i].split(",");
		for (j = 0; j < tdata.length; j++){
			trow = trow + "<td>" + tdata[j] + "</td>";
			}
		document.getElementById('table1').innerHTML = document.getElementById('table1').innerHTML + trow + "</tr>";
		}
	}

</script>
</head>

<body onload="initialize()">
<table id='table1'></table>
</body>
</html>
 
As far as I can tell, it just stops running the script. I get the "Request!" alert box in IE and Chrome, and I get the "Open!" alert box in Chrome only. Neither IE or Chrome makes it to the "Sent!" alert box. It doesn't seem like the script is stuck, it seems more like the script has stopped. Unfortunately, that's about the extent of my ability when it comes to troubleshooting JavaScript. Usually, when I find out where it's breaking I can figure out why, but not here. Any troubleshooting tips or suggestions would be most welcome!
 
I changed tack in my search and quickly found that IE and Chrome had built-in utilities for this (F12). Seems that IE is getting an access is denied error on the 'open' command, and Chrome is getting a "Cross origin requests are only supported for HTTP" on the 'send' command. Looking into those, I quickly realized that the underlying problem is that the full path for the "Supplies.csv" resource is file:\\...\Supplies.csv, and not http:\\...\Supplies.csv. I took the code from another script I had written on my webserver, and obviously didn't realize I needed the webserver for it to work. Now I'm looking into the File API to see if I can use that instead (
I'm trying to use an HTML and CSV file on a fileshare to facilitate our department's supplies inventory.
 
In the end, I switched to using an HTA w/ VBS. My main goal was to get it working in IE, but it seems that IE 8 doesn't support the File API I needed to use. I experimented with XDomainRequest, but that didn't look very promising either. Here's the final code, in case you're interested!

Code:
<html>
<head>
<title>
Supplies
</title>

<HTA:APPLICATION 
	APPLICATIONNAME="Supplies"
	SINGLEINSTANCE="no">

<style>
table, td {
	text-align: center;
	border-collapse: collapse;
	border: 1px solid black;
}
</style>

<script language="VBScript">
	Option Explicit

Function Initialize()
	DIM fso,supplies,table,trow,tdata,item
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set supplies = fso.OpenTextFile("Supplies.csv",1)

	table = ""
	Do Until supplies.AtEndOfStream
		trow = "<tr>"
		tdata = Split(supplies.ReadLine,",")
		For Each item In tdata
			trow = trow + "<td>" + item + "</td>"
		Next
		table = table + trow + "</tr>"
	Loop
	MainDisplay.innerHTML = "<table>" + table + "</table>"
End Function
</script>
</head>

<body id="MainDisplay" onload="Initialize()">
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top