Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-01-2012, 07:42 PM   PM User | #1
A-man
New to the CF scene

 
Join Date: Jul 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
A-man is an unknown quantity at this point
onreadystatechange disfunction

Hello,
I have a problem with a command in javascript, during an AJaX call.
I use the following code to detect when the readyState of a variable has changed so that I call another function.

Code:
myrequest.onreadystatechange=getData();

function getData() {

if (myrequest.readyState===4) {
if  (myrequest.status===200) {
var text=myrequest.responseText;
alert(text);

}

}

}
The problem is that the command onreadystatechange does not work properly because the function getData() gets loaded before the readyState of myrequest has changed. What can I do to fix this?
A-man is offline   Reply With Quote
Old 08-01-2012, 08:01 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 814
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Code:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
Procedure started
<script type="text/javascript">
var myrequest=new XMLHttpRequest;
myrequest.open("GET",'test.txt',true);
myrequest.onreadystatechange=getData;
function getData() {
	if (myrequest.readyState===4) {
		if (myrequest.status===200) {
			var text=myrequest.responseText;
			alert(text);
		}
	}
}
myrequest.send(null);

</script>
</body>
</html>
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
A-man (08-02-2012)
Old 08-02-2012, 09:42 AM   PM User | #3
A-man
New to the CF scene

 
Join Date: Jul 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
A-man is an unknown quantity at this point
Thank you very much DaveyErwin,
It worked thank you again.
A-man is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:25 PM.


Advertisement
Log in to turn off these ads.