roy_sanu
asked on
Ajax question
Hi,
I have a question when i was trying to run the index.html on the browser like firefox and chrome it is not displaying the data as Hello world. Let me what is the problem
Thanks
data.txt
script.js
index.html
I have a question when i was trying to run the index.html on the browser like firefox and chrome it is not displaying the data as Hello world. Let me what is the problem
Thanks
data.txt
script.js
index.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i understand that on juery one can run, but with ajax why it is not working on my system, it working on my friends machine
Are you running it on a server http://www.mywebsite.com/index.html or locally eg file://path/to/your/index. html
Do you see any error messages in the developer tools (press f12 and click on the console tab in Chrome)
Do you see any error messages in the developer tools (press f12 and click on the console tab in Chrome)
ASKER
I am running locally...
My code is like this
in the data.txt i have Hello world
In the script.js file i have
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---
var request;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.X MLHTTP");
}
request.open('GET', 'data.txt');
request.onreadystatechange = function() {
if ((request.readyState===4) && (request.status===200)) {
console.log(request);
document.writeln(request.r esponseTex t);
}
}
request.send();
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -
In the index.html i have
========================== ========== =========
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>JavaScript AJAX</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
========================== ========== ========== ========== ====
where is the problem i donot know..
I keep all the files at the same place or same folder
It is not showing any error nor it is printing as javaScript AJAX with Hello World.
it is blank.
My code is like this
in the data.txt i have Hello world
In the script.js file i have
--------------------------
var request;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.X
}
request.open('GET', 'data.txt');
request.onreadystatechange
if ((request.readyState===4) && (request.status===200)) {
console.log(request);
document.writeln(request.r
}
}
request.send();
--------------------------
In the index.html i have
==========================
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>JavaScript AJAX</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
==========================
where is the problem i donot know..
I keep all the files at the same place or same folder
It is not showing any error nor it is printing as javaScript AJAX with Hello World.
it is blank.
Back to your question, what I also think is missing is your code is running potentially before the page has finished rendering but that's also where jQuery comes in handy with the $(function() { ... } that JulianH has wrapped around the ajax code. It will wait until the page has loaded before running.