INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

XMLHttpRequest in IE and Chrome

XMLHttpRequest in IE and Chrome

(OP)
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!

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

RE: XMLHttpRequest in IE and Chrome

How does it fail?

Cheers,
Dian

RE: XMLHttpRequest in IE and Chrome

(OP)
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!

RE: XMLHttpRequest in IE and Chrome

(OP)
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 (http://www.html5rocks.com/en/tutorials/file/dndfil...).

I'm trying to use an HTML and CSV file on a fileshare to facilitate our department's supplies inventory.

RE: XMLHttpRequest in IE and Chrome

(OP)
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 --> HTA

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close