function hello()
{
var request = getXHR();
request.open("GET","A?value="+document.getElementById('a').value+"",true);
request.send(null);
request.onreadystatechange=function()
{
if(request.readyState==4)
{
if(request.status==200)
{
var val=request.responseText;
document.getElementById('a').value=val*10;
}
}
}
}
I found the above code..in an ajax tutorial...I cannot understand the reason for using
request.readyState==4
request.status==200
Can anyone explain me the reason for using this code segment?
function hello()
{
var request = getXHR();
request.open("GET","A?value="+document.getElementById('a').value+"",true);
request.send(null);
request.onreadystatechange=function()
{
if(request.readyState==4)
{
if(request.status==200)
{
var val=request.responseText;
document.getElementById('a').value=val*10;
}
}
}
}
I found the above code..in an ajax tutorial...I cannot understand the reason for using
request.readyState==4
request.status==200
Can anyone explain me the reason for using this code segment?
Share Improve this question edited Sep 28, 2013 at 10:58 Vbabey asked Sep 28, 2013 at 10:53 VbabeyVbabey 1131 gold badge2 silver badges10 bronze badges 5- 2 That is javascript, not java! And it has nothing to do with JSP or Servlets. Please fix your tags! – isnot2bad Commented Sep 28, 2013 at 10:55
- 2 Open google => "ajax readystate" => Take first result! – isnot2bad Commented Sep 28, 2013 at 10:57
- ok i did that...if I like that...I would have done..that I prefer someone help me here...that is y i came here...Google..next time I will do it... – Vbabey Commented Sep 28, 2013 at 10:59
- 1 you should search for an answer first by yourself before asking here in Stackoverflow. (How to Ask) – isnot2bad Commented Sep 28, 2013 at 11:04
- I googled "ajax readystate" and took the first result. It was this one. Time makes fools of us all. – Maxwell Evans Commented Sep 14, 2017 at 20:07
3 Answers
Reset to default 5Can anyone explain me the reason for using this code segment?
When a request to a server is sent, we want to perform some actions based on the response.
Refer State Description
0 The request is not initialized
1 The request has been set up
2 The request has been sent
3 The request is in process
4 The request is plete
In practice you almost never use any of them except for 4.
status
200: "OK"
404: Page not found
I hope.These will helps you.
When a request to a server is sent, we want to perform some actions based on the response.
The onreadystatechange event is triggered every time the readyState changes.
The readyState property holds the status of the XMLHttpRequest.
Three important properties of the XMLHttpRequest object
readyState: Holds the status of the XMLHttpRequest. Changes from 0 to 4:
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
status
200: "OK"
404: Page not found
The readystate basically means that the request has finished processing. 200 is the http status for OK. This means it is safe to try and access the data.
Really, use google!