•
•
•
•
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
![]() |
•
•
Join Date: May 2006
Posts: 57
Reputation:
Rep Power: 3
Solved Threads: 0
Guys,
I was going through the book called Ajax for Dummies and got the following example.
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
Error in firefox is:
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>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
getData is not defined
> 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:
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."
"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."
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 239
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.
Cheers,
Naveen
Edit: SOS, you beat me by a minute
<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*
*PM asking for help will be ignored*
•
•
Join Date: May 2006
Posts: 57
Reputation:
Rep Power: 3
Solved Threads: 0
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..
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 239
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*
*PM asking for help will be ignored*
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. ;-)
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.
"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."
"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."
•
•
Join Date: May 2006
Posts: 57
Reputation:
Rep Power: 3
Solved Threads: 0
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
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
access activation ajax api asp blogger blogging blogs code combo cross-browser javascript menu with few lines of code dani daniweb data developer development dreamweaver dropdownlist firefox gdata google gpl home html internet javascript javascript smooth scrolling scroll smoothly window document position javascript tab menu with rounded corners generator key linux microsoft module msdn net news office openbsd product programming reuse rss serial software source tags vista web wysiwyg xml
- Previous Thread: javascript sources
- Next Thread: var codeofheight



Linear Mode