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
roy_sanudeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Why not use JQuery - so much easier for AJax

<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  $('body').load('data.txt');
});
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobOwner (Aidellio)Commented:
jQuery is just javascript packaged up in easier to use functions as @JulianH has shown.  But what I like most about it is it's cross browser compatible.  It takes care of all the nuances between browser specific objects such as XMLHttpRequest or ActiveXObject("Microsoft.XMLHTTP") and allows you to do all this with a few lines.

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.
0
roy_sanudeveloperAuthor Commented:
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
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

RobOwner (Aidellio)Commented:
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)
0
roy_sanudeveloperAuthor Commented:
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.XMLHTTP");
}
request.open('GET', 'data.txt');
request.onreadystatechange = function() {
      if ((request.readyState===4) && (request.status===200)) {
            console.log(request);
            document.writeln(request.responseText);
      }
}
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.
0
RobOwner (Aidellio)Commented:
i thought so.  it's not going to work.  You'll need a server to get it to work, which can be localhost if you have iis installed

this is what i get:

my error
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.