PDA

View Full Version : Hello! Ajax question!



fpcreator2000
March 19th, 2007, 04:46
First of all, thank you to the DCemu crew for all of the emulation goodness that you brought ot my dreamcast and psp (especially the psp). Thank you very much.

I'm an aspiring web designer/developer, and I've run into a problem. I'm studying from this Ajax book, and as soon as I run the code for chapter 1, it throws me an error "access denied". here is the source code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ajax at work</title>

<script type="text/javascript">
//<![CDATA[
var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

function getData(dataSource, divID)
{
if(XMLHttpRequestObject) {
var obj = document.getElementById(divID);
XMLHttpRequestObject.open("GET", dataSource);

XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
obj.innerHTML = XMLHttpRequestObject.responseText;
}
}

XMLHttpRequestObject.send(null);
}
}
//]]>
</script>
</head>
<body>
<h1>Fetching data with Ajax</h1>
<form action="">
<input type="button" value="Display Message" onclick="getData('data.txt', 'targetDiv');" />
</form>
<div id="targetDiv">
<p>The fetched data will go here.</p>
</div>

</body>
</html>

------------------------------------
ERROR:

Internet Explorer throws me this "access denied" error.

Line: 20
Char: 11
Error: Access is denied
Code: 0
URL: file:///H:/Documents/Ajax/AjaxVBCombined/ch01/index.tml

------------------------------------

Any and all comments are welcomed. BTW, The Javascript is turned on. I tested this program on IE, and on Firefox.

Shane R. Monroe
March 19th, 2007, 06:39
Probably because browsers aren't supposed to be able to access your local hard drive through code.

Try uploading the sample, and data.txt file to a web host. Probably work then.

fpcreator2000
March 19th, 2007, 07:03
so if i provide a localhost type of environment, through a sever running on my machine, that should do the trick? I guess that would explain it. Similar protection as actionscript has on Flash. Good to know, I wish they explain these things in the book. Is as if they expect you to know the subject before reading the beginner's guide. Ironic, isn't it. Just like my MySQL teaches me how to create tables, and rows without teaching me how to create the database itself to house the tables and rows.

Thanks for the reply. It very helpful.

b8a
March 19th, 2007, 09:22
Your code should work without needing to be served. The only time I touch Windows is for testing web pages and I have no problem with AJAX pages without uploading them or using a local server. I know that there are different problems with different versions of Windows/IE, so for what it's worth, I have AJAX code working on both Windows 2000 and Windows 98 in Internet Explorer 6 and Firefox.

A few things you might want to try are:
-loosening your security settings (this was the first major hurdle I had to get over when I started testing in windows. The default IE security settings make it so that almost only very basic HTML works. Almost all semi-advanced JavaScript triggers security warnings)
-try entering the address to your file in a different manner. I can't remember the exact syntax for each, but if I try to enter a page's address through "browse..." and then selecting it, it tells me that I'm trying to access insecure information and asks me to validate the request, whereas if I enter the address directly (Z:\folder\folder\ajax_file.html), it'll load the xml data directly without any prompts.
-try the code on something other than a form. Try moving the same onclick handler over to "targetDiv" and see if it doesn't work. Or try it in a anchor.
-also, I doubt that this is the root of your problem, but I believe that

new ActiveXObject("Msxml2.XMLHTTP");
superceeds

new ActiveXObject("Microsoft.XMLHTTP");
so you may want to give that a go. At the very least, most AJAX tutorials recommend that you check for the first form before trying the second, ie:

try{
XMLHttpRequestObject =new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
XMLHttpRequestObject =new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
XMLHttpRequestObject =false;
}
}

fpcreator2000
March 19th, 2007, 17:25
I'm using a USBwebserver software and I'm using IE 7, and the latest FireFox. It works now. It was as Shane said that I do not have access to my local computer, but when I ran it under localhost:8080, it worked like a charm.

Thank you for all the input. I hope that one day I can come back, and provide to the community with my knowledge. Thanks again.