Help me out with this imple code pls

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: May 2006
Posts: 60
Reputation: kaushik259106 is an unknown quantity at this point 
Solved Threads: 0
kaushik259106 kaushik259106 is offline Offline
Junior Poster in Training

Help me out with this imple code pls

 
0
  #1
Jan 13th, 2008
Guys,
I was going through the book called Ajax for Dummies and got the following example.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Ajax at work</title>
  4. <script language = "javascript">
  5. var XMLHttpRequestObject = false;
  6. if (window.XMLHttpRequest) {
  7. XMLHttpRequestObject = new XMLHttpRequest();
  8. } else if (window.ActiveXObject) {
  9. XMLHttpRequestObject = new ActiveXObject(“Microsoft.XMLHTTP);
  10. }
  11. function getData(dataSource, divID)
  12. {
  13. if(XMLHttpRequestObject) {
  14. var obj = document.getElementById(divID);
  15. XMLHttpRequestObject.open(“GET”, dataSource);
  16. XMLHttpRequestObject.onreadystatechange = function()
  17. {
  18. if (XMLHttpRequestObject.readyState == 4 &&
  19. XMLHttpRequestObject.status == 200) {
  20. obj.innerHTML = XMLHttpRequestObject.responseText;
  21. }
  22. }
  23. XMLHttpRequestObject.send(null);
  24. }
  25. }
  26. </script>
  27. </head>
  28. <body>
  29. <H1>Fetching data with Ajax</H1>
  30. <form>
  31. <input type = "button" value = "Display Message"
  32. onclick = "getData('http://localhost:8090/jsp-examples/dates/data.txt',
  33. 'targetDiv')">
  34. </form>
  35. <div id="targetDiv">
  36. <p>The fetched data will go here.</p>
  37. </div>
  38. </body>
  39. </html>
It requires me to put a data file which exists in http://localhost:8090/jsp-examples/dates/data.txt

But when i execute i get errors.
Not able to figure out why.
Error in IE is
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. Line: 32
  2. Char: 1
  3. Error: Object Expected
  4. Code: 0
  5. URL: http://localhost:8090/jsp-examples/dates/index.html
Error in firefox is:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. getData is not defined
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,622
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Help me out with this imple code pls

 
0
  #2
Jan 13th, 2008
> XMLHttpRequestObject = new ActiveXObject(“Microsoft.XMLHTTP”);

If this is how your code looks in your Text Editor / IDE, you need to change the special character ” to double quotes ("). Avoid copy / pasting the code from your ebook into the editor since it might introduce such special characters.

A sample working snippet:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <meta http-equiv="Expires" content="0"> <!-- disable caching -->
  7. <title>Ajax Example</title>
  8. <script type="text/javascript" src="ajax.js"></script>
  9. <script type="text/javascript">
  10. function getXmlHttpRequest() {
  11. if (window.XMLHttpRequest) {
  12. return new XMLHttpRequest();
  13. }
  14. else if (window.ActiveXObject) {
  15. /*@cc_on @*/
  16. /*@if (@_jscript_version >= 5)
  17.   try {
  18.   return new ActiveXObject("Msxml2.XMLHTTP");
  19.   } catch (e) {
  20.   try {
  21.   return new ActiveXObject("Microsoft.XMLHTTP");
  22.   } catch (E) {
  23.   return null;
  24.   }
  25.   } @end @*/
  26. }
  27. else {
  28. return null;
  29. }
  30. }
  31. function getData(datasource, target) {
  32. var xhr = getXmlHttpRequest();
  33. xhr.open("GET", datasource, true);
  34. xhr.onreadystatechange = function() {
  35. if(xhr.readyState == 4) {
  36. if(xhr.status == 200) {
  37. document.getElementById(target).innerHTML = xhr.responseText;
  38. }
  39. else {
  40. alert("Some problem occured);
  41. }
  42. }
  43. }
  44. xhr.send(null);
  45. }
  46. </script>
  47. </head>
  48. <body>
  49. <form id="frm" name="frm" action="#">
  50. <div id="frmContainer">
  51. <div id="tgt"></div>
  52. <p></p>
  53. <input type="button" onclick="getData('data.txt', 'tgt');" value="Get Data">
  54. </div>
  55. </form>
  56. </body>
  57. </html>
  58.  
Last edited by ~s.o.s~; Jan 13th, 2008 at 3:55 am.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Help me out with this imple code pls

 
0
  #3
Jan 13th, 2008
Umm.. Change “ ” to " ". Put data.txt in the same folder where you have the ajax script. I don't know much of ajax, but, the following code worked for me without any errors.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Ajax at work</title>
  4. <script language = "javascript">
  5. var XMLHttpRequestObject = false;
  6. if (window.XMLHttpRequest) {
  7. XMLHttpRequestObject = new XMLHttpRequest();
  8. } else if (window.ActiveXObject) {
  9. XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  10. }
  11. function getData(dataSource, divID)
  12. {
  13. if(XMLHttpRequestObject) {
  14. var obj = document.getElementById(divID);
  15. XMLHttpRequestObject.open("GET", dataSource);
  16. XMLHttpRequestObject.onreadystatechange = function()
  17. {
  18. if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200 ) {
  19. obj.innerHTML = XMLHttpRequestObject.responseText;
  20. }
  21. }
  22. XMLHttpRequestObject.send(null);
  23. }
  24. }
  25. </script>
  26. </head>
  27. <body>
  28. <H1>Fetching data with Ajax</H1>
  29. <form>
  30. <input type = "button" value = "Display Message"
  31. onclick = "javascript: getData('data.txt',
  32. 'targetDiv')">
  33. </form>
  34. <div id="targetDiv">
  35. <p>The fetched data will go here.</p>
  36. </div>
  37. </body>
  38. </html>

Cheers,
Naveen

Edit: SOS, you beat me by a minute
Last edited by nav33n; Jan 13th, 2008 at 3:56 am.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 60
Reputation: kaushik259106 is an unknown quantity at this point 
Solved Threads: 0
kaushik259106 kaushik259106 is offline Offline
Junior Poster in Training

Re: Help me out with this imple code pls

 
0
  #4
Jan 13th, 2008
ya sos it was copy problem.. thanks for quick response..so which ide are you guys use for js.. since am using myEclise and it didnt showed any error..
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Help me out with this imple code pls

 
0
  #5
Jan 13th, 2008
If you are using firefox, you can use Error console. Error console lists all the errors(javascript & css) encountered in your page.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,622
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Help me out with this imple code pls

 
0
  #6
Jan 13th, 2008
Yes, using Firefox as your development browser along with some useful plugins like Web Development and Firebug, you can cut down a lot on your development time.

And it doesn't depend on the IDE, really. It's more about experience and observation. I pinned down the problem the moment I saw those weird quotes. And you should be aware and quite competent with the IDE. In the screenshot I am posting, compare the two statements, one which has the weird quotes and the one which has the right kind of quotes. Don't you see a highlighting difference?

And which Eclipse are you using by the way? For web development using Java, you should be using WTP instead of the normal version which is not HTML/CSS/JS aware.

PS: > Edit: SOS, you beat me by a minute
And that too with a well indented code. ;-)
Last edited by ~s.o.s~; Jan 13th, 2008 at 8:28 am.
Attached Thumbnails
untitled.JPG  
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 60
Reputation: kaushik259106 is an unknown quantity at this point 
Solved Threads: 0
kaushik259106 kaushik259106 is offline Offline
Junior Poster in Training

Re: Help me out with this imple code pls

 
0
  #7
Jan 14th, 2008
Ya thats true.. you need to know your ide better.. am learning..Thanks for these infos. am using myEclipse 6.0.1 it has eclipse 3 i hope.. i installed the standalone
Last edited by kaushik259106; Jan 14th, 2008 at 6:12 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: egbert is an unknown quantity at this point 
Solved Threads: 0
egbert egbert is offline Offline
Newbie Poster

Re: Help me out with this imple code pls

 
0
  #8
Dec 6th, 2008
Hi everyone

I've also been looking at the same code from the 'Ajax for Dummies' book (page 67). I'm running the code samples on an XAMPP installation with both files in the C:\xampp\htdocs folder which (appears) to have been set up as 'localhost'. However, there are 3 odd things about this code.

1 It will not work as stated unless I change the line
XMLHttpRequestObject.status == 200
to
XMLHttpRequestObject.status == 0

2 If instead of the line
onclick = "javascript: getData('data.txt',
'targetDiv')"
(which works) I put - as suggested in the book -
onclick = "javascript: getData('http://localhost/data.txt',
'targetDiv')"
- it does not work. It's as if it doesn't recognise local host.

3 If I move the html file and the data.txt file together to any folder outside of the local host altogether, it still works! I thought AJAX was only supposed to work out of a server environment?

Can anyone enlighten me?

<html>
<head>
<title>Ajax at work</title>
<script language = "javascript">
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>
<input type = "button" value = "Display Message"
onclick = "javascript: getData('data.txt',
'targetDiv')">
</form>
<div id="targetDiv">
<p>The fetched data will go here.</p>
</div>
</body>
</html>
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC