Ajax not working nowhere!

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 3017 Views - Last Post: 07 June 2011 - 12:01 PM

#1 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Ajax not working nowhere!

Posted 06 June 2011 - 02:06 AM

Hi, this is an Ajax example copied straight from a Ajax textbook. It seems to be perfectly ok (after all, it's straight from a text book), but doesn't do the expected trick in any browser.

<HTML>
<HEAD>
<TITLE>Ajax Joke of the Day Application</TITLE>
<script language = "javascript" type = "text/javascript">
var Request = false;
if (window.XMLHttpRequest) {
Request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
Request = new ActiveXObject("Microsoft.XMLHTTP");
}
function retrieveJoke(url, elementID) {
if(Request) {
var RequestObj = document.getElementById(elementID);
Request.open("GET", url, true);
Request.onreadystatechange = function()
{
if (Request.readyState == 4 && Request.status == 200) {
RequestObj.innerHTML = Request.responseText;
}
}
Request.send(null);
}
}
</SCRIPT>
</HEAD>
<BODY>
<H1>Where do bees go when they get married?</H1>
<FORM>
<INPUT type = "button" value = "Fetch Answer"
onclick = "retrieveJoke('joke.txt', 'DivTarget')">
</FORM>
<DIV id="DivTarget"> </DIV>
</BODY>
</HTML>



This file is located in the same folder with the following file named joke.txt:

Where do bees go after marriage?
Honeymoon.



When the button on the HTML file is clicked after it is loaded into the browser, the joke in the txt file is supposed to appear just below the button without any page refresh. But nothing is happening really, either in Firefox or in IE. The page remains as it is. Can somebody tell me what is wrong?

This post has been edited by cupidvogel: 06 June 2011 - 02:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Ajax not working nowhere!

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 02:09 AM

any messages in the Error Console?

you have made a synchronous request (3rd parameter in open()) thus you can’t use onreadystatechange.

This post has been edited by Dormilich: 06 June 2011 - 02:13 AM

Was This Post Helpful? 1
  • +
  • -

#3 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 02:13 AM

You mean Firebug? I do have Firebug, but dunno what kind of error you are talking about.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 02:15 AM

I donít mean Firebug (though thatís always good to have).

you simply donít have the asynchronous request type you need for that code (see above)
Was This Post Helpful? 0
  • +
  • -

#5 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 02:20 AM

Sorry, I actually used true earlier, when it didn't work, I tried false, and when it still didn't work, I copy-pasted it without changing that! It wasn't working even with true.

There, I have revised the code (replacing the false with true), and like I said, it's still not working.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 02:21 AM

if itís not working with true, there should be a message in the error console.
Was This Post Helpful? 0
  • +
  • -

#7 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 02:24 AM

Where is the error console? Is it in the status bar in the left hand bottom corner? If so, then I am not getting any error message in Firefox, but I am getting a message reading ERROR in IE.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 02:29 AM

Iíve given you a link that describes where you find the error console in all major browsers.
Was This Post Helpful? 0
  • +
  • -

#9 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 03:12 AM

In Firefox, I am not getting any error signal. In IE I am, which I mentioned is on the left hand bottom corner in the status bar. Double clicking that is giving the error details as "Access denied" at line 14, which you can see from the code is :

Request.open("GET", url, true);



So what is the error?
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 03:42 AM

you are not allowed to open the file. you need to adjust the file permissions accordingly.
Was This Post Helpful? 0
  • +
  • -

#11 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 03:55 AM

Why am I not permitted? I heard Firefox prevents opening files from different domain, but here I am opening the file from the same domain as my app. So why should I be denied permission in Firefox as well as IE? What is the workaround? Gosh, I am just beginning to learn Ajax, and can't get to run even the simplest of codes!
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 03:57 AM

View Postcupidvogel, on 06 June 2011 - 12:55 PM, said:

What is the workaround?

change the file permissions.

View Postcupidvogel, on 06 June 2011 - 12:55 PM, said:

Gosh, I am just beginning to learn Ajax, and can't get to run even the simplest of codes!

that’s more of a server issue than from AJAX.

This post has been edited by Dormilich: 06 June 2011 - 03:57 AM

Was This Post Helpful? 1
  • +
  • -

#13 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 03:59 AM

How do I change file permissions? And speaking of server issue, I am not really using any server or something. Both the XML and HTML files are located in a folder on my desktop, from there I am loading the HTML file in the browser.
Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 04:02 AM

I’m not a windows guy so I don’t know.

that explains why nothing happens, a local file does not give you a HTTP status code (because only servers do that) i.e. if (XMLHttpRequest.status === 0)

… that should have been mentioned in your text book.

This post has been edited by Dormilich: 06 June 2011 - 04:04 AM

Was This Post Helpful? 2
  • +
  • -

#15 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Ajax not working nowhere!

Posted 06 June 2011 - 04:06 AM

But in that book it is written that:

Actually, since all you are working with at this point is HTML, you can test your application directly from your desktop without first uploading its HTML files to your web server. To do this, all you have to do is start up your web browser and instead of loading index.html by specifying its URL, you click on the File menu and select the Open command and then specify the name and location of the HTML file. To work, a copy of the joke.txt file must reside in the same folder as index.html file.

So how do I learn Ajax by executing codes (like I learn jQuery or Javascript through codes from my desktop) without using server?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3