/    Sign up×
Community /Pin to ProfileBookmark

XMLhttprequest stopping and getting access denied

I am having some security problems and need some help..

a little backstory.. I had the script running fine on my computer. . transfered them to my laptop, and the script still found and parses the xml like it should. however, i zipped them and sent it back to myself and now all the values come back undefined. .

here is the script. .

[QUOTE]

function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xhttp.open(“GET”,dname,false);
xhttp.send();
return xhttp.responseXML;
}

var xmlDoc = loadXMLDoc(“nhl_standings_xml.xml”);
var x = xmlDoc.getElementsByTagName(“nhlall”);

[/QUOTE]

fairly straight forward and simple.. but values of x[0].textContent is coming back undefined.. i have all files (html, js, and xml) in the same folder on my desktop. they worked perfectly fine before when they were just on my desktop.. but now that won’t even work. :/ help please

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@TcobbFeb 01.2011 — May I suggest that after the line: [CODE]xhttp.send();[/CODE]
you include the lines:
[CODE]alert(xhttp.readyState + "---" + xhttp.status); //its its anything other than '4---200' this will give you the key to the error
alert("the xml data is: " + xhttp.responseXML); //has it sent any XML data at all
alert("the response text is: " + xhttp.responseText); did it send any text back[/CODE]


Sometimes it is the case that that the responseXML will be empty, or undefined, but the information will be in the reponseText.

PS-- Sorry for the hasty response--I was looking at the content of your post rather than the title. Did you indeed get an xhttp.status code that indicated that access was denied?
Copy linkTweet thisAlerts:
@shawnkuauthorFeb 01.2011 — hello, it is very smart to test it in this way, but i received no alerts in the script. ?

it is very strange indeed. and my brain is about to bleed from picking over it for two days. ;p

IE and opera(as well as chrome and i assume firefox) are all giving me security errors at the line i stated above (when i pull up dev tools debugger).. a quick search on google and i pull up that i am crossing domains, yet all my files are local and in the same directory. :?

would setting up apache and testing it help at all do you think? it was all working perfectly before i zipped them
Copy linkTweet thisAlerts:
@shawnkuauthorFeb 01.2011 — disregard my last message please:

i got the following results

1 4---0

2 the xml data is: [object XMLDocument]

3 the response text is:
Copy linkTweet thisAlerts:
@TcobbFeb 01.2011 — Usually when the 'status' equals zero this means it came from a local file rather than being processed by a server.

And yes--it might help to fire up Apache on your box and run it through there.

Another avenue to pursue is to compare the files--the original ones and the ones that were produced from having their zipped versions unzipped.
Copy linkTweet thisAlerts:
@shawnkuauthorFeb 01.2011 — the files are coming locally - is there something wrong with that? why did it work fine a few days ago on all browsers without touching code? is it possible the browsers updated since then? sorry for so many question.. so frustrated.. should i somehow process them through a local server instead? (i will try apache here in a bit -- see if anything works out)

interestingly, what i said beforehand isn't true. everything is working as it should in firefox, but not chrome, ie, or opera .. ?
Copy linkTweet thisAlerts:
@KorFeb 02.2011 — the files are coming locally - is there something wrong with that? [/QUOTE]
AJAX requests (same as any classical submit) will not work locally, unless you install a server (like Apache or IIS) on your computer and run your files under that server.
Copy linkTweet thisAlerts:
@cluefulFeb 02.2011 — AJAX requests (same as any classical submit) will not work locally, unless you install a server (like Apache or IIS) on your computer and run your files under that server.[/QUOTE]
You'll need a server to run PHP etc, but I have no trouble running AJAX requests under the [FONT="Courier New"]file:///[/FONT] protocol.

This routine displays its own source:
[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
<title>Load Own Source File</title>
</head>
<body>
<form action='#'>
<p>
<textarea id='ta1' cols='60' rows='20'></textarea>
</p>
</form>
<script type="text/javascript">
/* <![CDATA[ */

req = new XMLHttpRequest();

req.onreadystatechange = function()
{
if( this.readyState == 4 && this.status == ( /$http/.test( location.href ) ? 200 : 0 ) )
document.getElementById( 'ta1' ).value = this.responseText;
}

req.open( "GET", location.href, true );

req.send( null );

/* ]]> */
</script>
</body>
</html> [/CODE]
Copy linkTweet thisAlerts:
@KorFeb 02.2011 — Well, yes. Some browsers accept request even when the status of the document is 0, not 200. But somehow it looks safer to test locally under an installed server.

Now, back to the main problem:

but values of x[0].[COLOR="Red"]textContent[/COLOR] is coming back undefined
[/quote]

I bet you are using an old version of IE on your laptop. Either upgrade your IE, or better use alternatively the innerText IE property. Rough code:
<i>
</i>var text
if(element.textContent){
text=element.textContent;
}
else if(element.innerText){
text=element.innerTextt;
}
Copy linkTweet thisAlerts:
@shawnkuauthorFeb 03.2011 — all browsers are runnign thsi script fine but IE8.. i ahve them all hosted locally on apache
Copy linkTweet thisAlerts:
@TcobbFeb 03.2011 — And what kind of xhttp.readyState and xhttp.status messages are you getting on IE8?
Copy linkTweet thisAlerts:
@shawnkuauthorFeb 03.2011 — just tried oth of them and interestingly it's giving me a readystate of 4, and a status of 200 on all browser's including ie. doesnt that mean it should be working loading and executing properly?
Copy linkTweet thisAlerts:
@shawnkuauthorFeb 03.2011 — i have been reading that IE doesnt really like .textContent , which i am using .. is there any good alternatives to it?
Copy linkTweet thisAlerts:
@TcobbFeb 03.2011 — I don't really know what '.textContent' is--what are the values of your xhttp.responseText and xhttp.responseXML? That's the key. Your original problem was getting the data to begin with. That now apparently is solved.

What you do with the data is now up to you.
Copy linkTweet thisAlerts:
@shawnkuauthorFeb 03.2011 — it seems you are right.

the value in order as you listed them are '4' and '200' .. which seems like it's normal..
Copy linkTweet thisAlerts:
@shawnkuauthorFeb 04.2011 — sorry for my last message.. i misunderstood you..

the response to xhttp.responseText and xhttp.responseXML came back as it should.. the same in IE as in all other broswers.. however, i think IE just doesnt like the way im pulling text out of my variables.. like this..

var PittLoses = x[i+4].textContent;
var PittOTWins = x[i+5].textContent;


its really the only thing i can think of at this point. thanks for all your help man.
×

Success!

Help @shawnku spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...