jxbma
asked on
Why am I getting an XMLHttpRequest Exception 101 when attempting to use AJAX to retieve a text file in HTML5 page with javascript?
Hi:
I'm new to the world of HTML5, Javascript, jquery and AJAX.
I'm trying to recreate a simple example from a tutorial I found online.
The example is supposed to grab the contents from a text file on the local disk
and populate the contents of a div with the text.
I keep running into the following exception:
-------------------------- ---------- ---------- ---------- ----
Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101
I see the exception occur on the Send().
I'm debugging using the Chrome debugging tools.
I'm breaking on the .send(null) line.
I hit F10.
I wait and nothing happens. I hit F10 again and I see the error.
I've tried setting asynch flag to both true and false on the .open(), but I get the same results.
It's not clear what I'm doing wrong and I have no idea on how to debug this issue.
Is it a permissions thing? I'm attempting to run this in Chrome.
Here is the snippet of javascript:
-------------------------- ---------- --------
var xhrObject = new XMLHttpRequest();
if (window.XMLHttpRequest) {
xhrObject = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhrObject = new ActiveXObject("Microsoft.X MLHTTP");
}
function populateTextContentDiv(){
var externalTextFile = "TextContentSample.txt";
// if we can use AJAX
if(xhrObject) {
// create a new variable that traverses the DOM and finds the <div> tag we put into our second parameter
var pageText = document.getElementById("T extContent ");
// use AJAX to place a text file we put in our first parameter
xhrObject.open("GET", externalTextFile, true);
// if our AJAX variable found our data and the server is ready to send it out, bundle our data as a text string
xhrObject.onreadystatechan ge = function () {
if(xhrObject.readyState == 4 && xhrObject.status == 200) {
pageText.innerHTML = xhrObject.responseText;
}
}
// send our data out
xhrObject.send(null);
}
}
I'm new to the world of HTML5, Javascript, jquery and AJAX.
I'm trying to recreate a simple example from a tutorial I found online.
The example is supposed to grab the contents from a text file on the local disk
and populate the contents of a div with the text.
I keep running into the following exception:
--------------------------
Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101
I see the exception occur on the Send().
I'm debugging using the Chrome debugging tools.
I'm breaking on the .send(null) line.
I hit F10.
I wait and nothing happens. I hit F10 again and I see the error.
I've tried setting asynch flag to both true and false on the .open(), but I get the same results.
It's not clear what I'm doing wrong and I have no idea on how to debug this issue.
Is it a permissions thing? I'm attempting to run this in Chrome.
Here is the snippet of javascript:
--------------------------
var xhrObject = new XMLHttpRequest();
if (window.XMLHttpRequest) {
xhrObject = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhrObject = new ActiveXObject("Microsoft.X
}
function populateTextContentDiv(){
var externalTextFile = "TextContentSample.txt";
// if we can use AJAX
if(xhrObject) {
// create a new variable that traverses the DOM and finds the <div> tag we put into our second parameter
var pageText = document.getElementById("T
// use AJAX to place a text file we put in our first parameter
xhrObject.open("GET", externalTextFile, true);
// if our AJAX variable found our data and the server is ready to send it out, bundle our data as a text string
xhrObject.onreadystatechan
if(xhrObject.readyState == 4 && xhrObject.status == 200) {
pageText.innerHTML = xhrObject.responseText;
}
}
// send our data out
xhrObject.send(null);
}
}
what is the URL?
ASKER
This is the URL of my main page.
file:///C:/Dev%20Projects/ Project1/F rontEnd/We bPages/Fra mework/Pro ject1/inde x.html#
I'm simply loading this URL into Chrome and working locally.
Thanks,
JohnB
file:///C:/Dev%20Projects/
I'm simply loading this URL into Chrome and working locally.
Thanks,
JohnB
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Is here an easy way to set that up locally?
I'm pretty green at this...
Thanks,
JohnB
I'm pretty green at this...
Thanks,
JohnB
download apache httpd : http://httpd.apache.org/download.cgi
or use your IIS if you're on windows : http://www.iis.net/learn/install/installing-iis-7/installing-iis-on-windows-vista-and-windows-7
or use your IIS if you're on windows : http://www.iis.net/learn/install/installing-iis-7/installing-iis-on-windows-vista-and-windows-7
If you don't what to run from a webserver, that is not http/https from file:// protocol just like you were doing
try something like this.
hope this helps,
kiranvj
try something like this.
xhrObject.onreadystatechange = function () {
if((xhrObject.readyState == 4 && xhrObject.status == 200) || (xhrObject.status == 200 && document.location.protocol == "file:")) {
pageText.innerHTML = xhrObject.responseText;
}
hope this helps,
kiranvj
You can use xampp to establish a local webserver where you can simulate an ajax call.
ASKER
Spot on. I was having Cross Domain issues.<br />I created in instance in IIS and and running though that fine.<br /><br />Thanks,<br />JB
You're welcome, thanks for the points. That article explains a lot of things.
ASKER