By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,324 Members | 536 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,324 IT Pros & Developers. It's quick & easy.

XMLHttpRequest function not work

P: 34
Hi, i have created a XMLHttpRequest function, but it didn't work, this is my code
Expand|Select|Wrap|Line Numbers
  1. function CheckAccount (acc) {
  2. CreateXMLHttpRequest();
  3.   if (ready == 1) {
  4.     if (!xmlHttp) {
  5.     alert('Please upgrade your browser');
  6.     }else{
  7.     var content = "AJAXQueryAccount=" + acc;
  8.     alert('set content!');
  9.     xmlHttp.onreadystatechange=CatchResult;
  10.     alert('set ors!');
  11.     xmlHttp.open("POST", "?a=r");
  12.     alert('opended!');
  13.     xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  14.     alert('req. header set!');
  15.     xmlHttp.send(content);
  16.     alert('sent!');
  17.     }
  18.   }
  19. }
  20.  
  21. function CatchResult() {
  22. alert('ran CatchResult function!');
  23. if(xmlHttp.readyState == 4) {
  24.   if(xmlHttp.status == 200) {
  25.     if(xmlHttp.responseText == '1') {
  26.     var x = document.getElementById('reg_remind_2');
  27.     x.style.position = 'absolute';
  28.     x.style.border = '#000 1px solid';
  29.     x.style.background = '#777';
  30.     x.style.marginBottom = '750px';
  31.     x.style.marginLeft = '375px';
  32.     x.style.width = '265px';
  33.     x.style.height = '15px';
  34.     x.style.zIndex = '100';
  35.     x.style.color = '#0F0';
  36.     x.style.textAlign = 'center';
  37.     x.innerHTML = 'Congratulation, this account is available!';
  38.     document.getElementById('reg_account').style.border = '1px #0F0 solid';
  39.     }else if(xmlHttp.responseText == '0') {
  40.     var x = document.getElementById('reg_remind_2');
  41.     document.getElementById('reg_account').style.border = '1px #F00 solid';
  42.     x.style.position = 'absolute';
  43.     x.style.border = '#000 1px solid';
  44.     x.style.background = '#777';
  45.     x.style.marginTop = '-390px';
  46.     x.style.marginLeft = '375px';
  47.     x.style.width = '265px';
  48.     x.style.height = '15px';
  49.     x.style.zIndex = '100';
  50.     x.style.color = '#F00';
  51.     x.style.textAlign = 'center';
  52.     x.innerHTML = 'Sorry, this account is unavailable!';
  53.     }
  54.   }
  55. }
  56. }
  57.  
The program didn't run "CatchResult()" function!(it only alert to "sent!")

I have also tried type "javascript:CatchResult();", it also didn't alert "ran CatchResult function!"!

Any one have solution?

Thanks!
Aug 6 '10 #1
Share this Question
Share on Google+
13 Replies

Dormilich
Expert Mod 5K+
P: 8,639
as far as I know, you first open the connection, then attach the handler and then send the asyncronous request. in case of a syncronous request, you would execute the handler after .send().
Aug 6 '10 #2

P: 34
It didn't work, it didn't run CatchResult() !

Expand|Select|Wrap|Line Numbers
  1. function CheckAccount (acc) {
  2. CreateXMLHttpRequest();
  3.   if (ready == 1) {
  4.     if (!xmlHttp) {
  5.     alert('Please upgrade your browser');
  6.     }else{
  7.     var content = "AJAXQueryAccount=" + acc;
  8.     alert('set content!');
  9.     xmlHttp.open("POST", "?a=r");
  10.     alert('opended!');
  11.     xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  12.     alert('REQ. HEADER SET!');
  13.     xmlHttp.onreadystatechange=CatchResult;
  14.     alert('set ors!');
  15.     xmlHttp.send(content);
  16.     alert('sent!');
  17.     }
  18.   }
  19. }
  20.  
  21. function CatchResult() {
  22. alert('ran CR function!');
  23. if(xmlHttp.readyState == 4) {
  24.   if(xmlHttp.status == 200) {
  25.     if(xmlHttp.responseText == '1') {
  26.     var x = document.getElementById('reg_remind_2');
  27.     x.style.position = 'absolute';
  28.     x.style.border = '#000 1px solid';
  29.     x.style.background = '#777';
  30.     x.style.marginBottom = '750px';
  31.     x.style.marginLeft = '375px';
  32.     x.style.width = '265px';
  33.     x.style.height = '15px';
  34.     x.style.zIndex = '100';
  35.     x.style.color = '#0F0';
  36.     x.style.textAlign = 'center';
  37.     x.innerHTML = 'Congratulation, this account is available!';
  38.     document.getElementById('reg_account').style.border = '1px #0F0 solid';
  39.     }else if(xmlHttp.responseText == '0') {
  40.     var x = document.getElementById('reg_remind_2');
  41.     document.getElementById('reg_account').style.border = '1px #F00 solid';
  42.     x.style.position = 'absolute';
  43.     x.style.border = '#000 1px solid';
  44.     x.style.background = '#777';
  45.     x.style.marginTop = '-390px';
  46.     x.style.marginLeft = '375px';
  47.     x.style.width = '265px';
  48.     x.style.height = '15px';
  49.     x.style.zIndex = '100';
  50.     x.style.color = '#F00';
  51.     x.style.textAlign = 'center';
  52.     x.innerHTML = 'Sorry, this account is unavailable!';
  53.     }
  54.   }
  55. }
  56. }
  57.  
Aug 6 '10 #3

Dormilich
Expert Mod 5K+
P: 8,639
are you sure, "?a=r" points to the right location?
Aug 6 '10 #4

P: 34
yes, i am sure because i have tried to create a simple html form and method = post, action = ?a=r !

But the main point is the script didn't run the "CatchResult()" function!

It didn't alert "ran CR function!"!
Also, when i tried enter javascript:CatchResult(); , it also didn't alert that message!

Anyone have solution?

Thanks!
Aug 6 '10 #5

Dormilich
Expert Mod 5K+
P: 8,639
then I’d like to see myself on a live page, so I can run Firebug.

have you verified, your server script is running correctly? (if you don’t get a response, how should a readystatechange event be fired)
Aug 6 '10 #6

P: 34
i have pmed the link!
Aug 6 '10 #7

P: 34
Anyone have solution?
Aug 9 '10 #8

Dormilich
Expert Mod 5K+
P: 8,639
from what I can see, the readystatechange handler doesn’t get assigned.
Aug 9 '10 #9

P: 34
so how to fix that?

thanks.
Aug 9 '10 #10

P: 34
ANYONE CAN HELP??

I THINK not the onreadystatechange fired, is the function "CatchResult()" fired!
Aug 10 '10 #11

JKing
Expert 100+
P: 1,206
Have you tried setting the async parameter to true when you use the xmlhttp.open()?
Expand|Select|Wrap|Line Numbers
  1. function CheckAccount (acc) {
  2. CreateXMLHttpRequest();
  3.   if (ready == 1) {
  4.     if (!xmlHttp) {
  5.     alert('Please upgrade your browser');
  6.     }else{
  7.     var content = "AJAXQueryAccount=" + acc;
  8.     alert('set content!');
  9.     xmlHttp.onreadystatechange=CatchResult;
  10.     alert('set ors!');
  11.     xmlHttp.open("POST", "?a=r", true);
  12.     alert('opended!');
  13.     xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  14.     alert('REQ. HEADER SET!');
  15.  
  16.     xmlHttp.send(content);
  17.     alert('sent!');
  18.     }
  19.   }
  20. }
  21.  
  22. function CatchResult() {
  23. alert('ran CR function!');
  24. if(xmlHttp.readyState == 4) {
  25.   if(xmlHttp.status == 200) {
  26.     if(xmlHttp.responseText == '1') {
  27.     var x = document.getElementById('reg_remind_2');
  28.     x.style.position = 'absolute';
  29.     x.style.border = '#000 1px solid';
  30.     x.style.background = '#777';
  31.     x.style.marginBottom = '750px';
  32.     x.style.marginLeft = '375px';
  33.     x.style.width = '265px';
  34.     x.style.height = '15px';
  35.     x.style.zIndex = '100';
  36.     x.style.color = '#0F0';
  37.     x.style.textAlign = 'center';
  38.     x.innerHTML = 'Congratulation, this account is available!';
  39.     document.getElementById('reg_account').style.border = '1px #0F0 solid';
  40.     }else if(xmlHttp.responseText == '0') {
  41.     var x = document.getElementById('reg_remind_2');
  42.     document.getElementById('reg_account').style.border = '1px #F00 solid';
  43.     x.style.position = 'absolute';
  44.     x.style.border = '#000 1px solid';
  45.     x.style.background = '#777';
  46.     x.style.marginTop = '-390px';
  47.     x.style.marginLeft = '375px';
  48.     x.style.width = '265px';
  49.     x.style.height = '15px';
  50.     x.style.zIndex = '100';
  51.     x.style.color = '#F00';
  52.     x.style.textAlign = 'center';
  53.     x.innerHTML = 'Sorry, this account is unavailable!';
  54.     }
  55.   }
  56. }
  57. }
  58.  
Aug 10 '10 #12

P: 34
no use...

i want fix this asap, anyone have solution?
Aug 11 '10 #13

P: 34
help, it's one week already!

ANYONE HAVE SOLUTION?
Aug 18 '10 #14

Post your reply

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