How to correct getting xmlhttp.readyState = 0


In a AJAX code I have, I am getting xmlhttp.readyState = 0 rather than 4 which of course
is the correct result.

I use the same AJAX code in other pages and it always returns the correct state of 4.
But somehow on this new page it keeps returning 0.
I have put all sort of debugging code and cannot see why it is doing that!

What suggestions do you have to fix mlhttp.readyState = 0?

BTW, you can see the page here:

And the item in question that is firing the AJAX code is when user select from the
“Report As” selection drop down list in front of the “Answer This Question” button.
As you can see it has debug alert code associated with it currently.



I doubt that your working code has a line like that.


You are actually right. In the other code it is like:

so there are not () after stateChanged.

But since stateChanged is a function, that is:

function stateChanged()

if (xmlhttp.readyState == 4) {

var result = xmlhttp.responseText;
  if (result == 2) {
	document.getElementById('report_div').style.display = 'block';
	document.getElementById('report_div').innerHTML = 'OK';


I thought I could include the () after stateChanged so that I can pass then variables to it from function that is called from the Web page.
Put another way I need to pass variable to stateChanged so that it knows which DIV to update. So the value of report_div as is currently hard coded in stateChanged needs to be given to it. How can one do this?



Just wanted to report to you that I found a great way to pass the DIV value from Web page to AJAX function for use later by function stateChanged().
I got the job done by setting the DIV value as a global variable which is then retrieved by function stateChanged().
FYI, using these 2 function:

function setValue(div_val)
window.myValue = div_val;

function getValue()
return window.myValue;

Sweet :slight_smile:

as a lemon

xmlhttp.onreadystatechange = function(){ stateChanged( div_val ); }

Actually I got thinking that would this method I have found for saving values between 2 JS functions work OK in
case of Apple and other non-Windows based devices?
Since the method is based on:

window.myValues = [report, rpt_for, qid, aid, div_name, votes];

and that window. maybe working on Windows based PCs only!

Do you know?


Yes it works on all platforms, however ideally you should try to get away from using global variables.
Is there any reason that your readystatechange handler can’t run the statements necessary to get the required value?

Ok, it is good to know that window.myValues works the same on all platforms.

About your questions, I just think this way is much cleaner.