473,382 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

AJAX readyState is always 1

Hi

I have been attempting to check that a username exists in a text file using JavaScript. I call the CheckUserExists() method (below) passing in the username. However the readyState is always 1 so the file does not get read.

The webpage I am calling it from is something like this: www.mysite.co.uk/html/home.html

and the text file is located at: www.mysite.co.uk/html/resources/somefile.txt

I know really I should use server-side scripts but this is not possible so I need to use JavaScript.

Anyone got any ideas where I am going wrong? This has been doing my head in all day so hopefully someone knows what I have done wrong.
Expand|Select|Wrap|Line Numbers
  1. var name;
  2. var file;
  3.  
  4. function CheckUserExists(username)
  5. {
  6.     name=username;
  7.  
  8.     if(window.XMLHttpRequest)
  9.     {
  10.         file = new XMLHttpRequest();
  11.     }
  12.     else if(window.ActiveXObject)
  13.     {
  14.         file = new ActiveXObject("Microsoft.XMLHTTP");
  15.     }
  16.     else
  17.     {
  18.         alert("Error XMLHTTP Not Supported");
  19.     }
  20.  
  21.     file.open("GET", "resources/somefile.txt", true);
  22.     file.onreadystagechange = ReadFile(); 
  23.     file.send(null);
  24. }
  25.  
  26. function ReadFile()
  27. {
  28.     if(file.readyState == 4 || file.readyState == "complete") //Check file is ready to parse
  29.     {
  30.         if(file.status == 200) //Check file was found
  31.         {
  32.             //Read file etc...
  33.         }    
  34.     }
  35.     else
  36.     {
  37.         alert(file.readyState);
  38.     }
  39. }
  40.  
Feb 2 '10 #1

✓ answered by acoder

Change line 22 to:
Expand|Select|Wrap|Line Numbers
  1. file.onreadystagechange = ReadFile;

4 3417
acoder
16,027 Expert Mod 8TB
Change line 22 to:
Expand|Select|Wrap|Line Numbers
  1. file.onreadystagechange = ReadFile;
Feb 2 '10 #2
Thanks for the reply, I have made that change that you suggested and it now works.

Very strange though as I tried it earlier and it didn't work but after going for dinner and coming back then making the change again it works.
Feb 2 '10 #3
acoder
16,027 Expert Mod 8TB
Perhaps you were viewing a cached version of the page?

Anyway, glad to hear that it's now working.
Feb 2 '10 #4
It won't have been a cached version of the page as I tried clearing the cache a few times and then refreshing the page.

Very pleased it's working it had been getting on my nerves. Thanks again for your help.
Feb 2 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Eric Wallstedt | last post by:
I have a page that "logs" changes made to input fields using ajax to pass data to a cgi. I use POST and it works fine most of the time (all the time in IE). But it fails when I get the data from...
4
by: Frances | last post by:
I literally started learning AJAX just last weekend.. I have this page, http://www.francesdelrio.com/ajax/db2.html, where I'm essentially doing what's here,...
6
by: Nico VanHaaster | last post by:
Hello all, I have run across an issue with IE 6.0+. I have a page that makes an XMLHttpRequest to the webserver to update a report on the page. The first time you hit the refresh report button...
5
by: steve.chambers | last post by:
I'm sure this q must have been asked before but I'm really struggling to find the answer anywhere so have finally given up and will consult the usenet community - hopefully there's someone out...
4
by: slebetman | last post by:
On Jun 5, 9:36 pm, sheldonlg <sheldonlgwrote: You can of course use closure to pass the required parameter: var hideIt; // the variable you wish to pass ajax.onreadystatechange = function () {...
22
by: sheldonlg | last post by:
I am looking for a clean solution to a problem that I solved in, what I call, a "dirty" way. Here is what I want to do. I have a dropdown list. Clicking on an item in the dropdown list invokes...
2
by: mndprasad | last post by:
Hi friends, Am new to AJAX coding, In my program am going to have two texbox which going to implent AJAX from same table. One box is going to retrieve the value of other and vice...
6
by: KDawg44 | last post by:
Hi, My responseXML is always null on my AJAX call. When I browse directly to the PHP script I am calling, the XML file shows up just fine. I have read that if a returned XML file is not...
1
by: bgold12 | last post by:
I have a page that calls a javascript function every second using setInterval(): ... <body onload="setInterval('UpdateMessages(1);', 1000 );"> ... In UpdateMessages(), I create an AJAX...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.