User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 402,064 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,481 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1109 | Replies: 6 | Solved
Reply
Join Date: May 2006
Posts: 57
Reputation: kaushik259106 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
kaushik259106 kaushik259106 is offline Offline
Junior Poster in Training

Help Help me out with this imple code pls

  #1  
Jan 12th, 2008
Guys,
I was going through the book called Ajax for Dummies and got the following example.
<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 = "getData('http://localhost:8090/jsp-examples/dates/data.txt',
'targetDiv')">
</form>
<div id="targetDiv">
<p>The fetched data will go here.</p>
</div>
</body>
</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
Line: 32
Char: 1
Error: Object Expected
Code: 0
URL: http://localhost:8090/jsp-examples/dates/index.html
Error in firefox is:
getData is not defined
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Help me out with this imple code pls

  #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:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="Expires" content="0"> <!-- disable caching -->
    <title>Ajax Example</title>
    <script type="text/javascript" src="ajax.js"></script>
    <script type="text/javascript">
        function getXmlHttpRequest() {
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            /*@cc_on @*/ 
            /*@if (@_jscript_version >= 5)
            try {
                return new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                } catch (E) {
                    return null;
                }
            } @end @*/ 
        }
        else {
            return null;
        }
    }
    function getData(datasource, target) {
        var xhr = getXmlHttpRequest();
        xhr.open("GET", datasource, true);
        xhr.onreadystatechange = function() {
            if(xhr.readyState == 4) {
                if(xhr.status == 200) {
                    document.getElementById(target).innerHTML = xhr.responseText;
                }
                else {
                    alert("Some problem occured);
                }
            }
        }
        xhr.send(null);
    }
    </script>
</head>
<body>
<form id="frm" name="frm" action="#">   
<div id="frmContainer">
    <div id="tgt"></div>
    <p></p>
    <input type="button" onclick="getData('data.txt', 'tgt');" value="Get Data">
</div>
</form>
</body>
</html>
Last edited by ~s.o.s~ : Jan 13th, 2008 at 2:55 am.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 239
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Help me out with this imple code pls

  #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.
<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>

Cheers,
Naveen

Edit: SOS, you beat me by a minute
Last edited by nav33n : Jan 13th, 2008 at 2:56 am.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: May 2006
Posts: 57
Reputation: kaushik259106 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
kaushik259106 kaushik259106 is offline Offline
Junior Poster in Training

Re: Help me out with this imple code pls

  #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  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 239
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Help me out with this imple code pls

  #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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Help me out with this imple code pls

  #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 7:28 am.
Attached Images
File Type: jpg untitled.JPG (54.6 KB, 4 views)
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: May 2006
Posts: 57
Reputation: kaushik259106 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
kaushik259106 kaushik259106 is offline Offline
Junior Poster in Training

Re: Help me out with this imple code pls

  #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 5:12 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 12:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC