/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Ajax response status is 0 in asynchronous mode, but 200 in synchronous

When I try to fetch a page asyncronously I get a status 0 and the response text is empty:

[code=php]var loaderImage = document.getElementById(“loader”);
loaderImage.style.display = “inline”;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status == 200) {
response = xmlhttp.responseText;
alert(response);
loaderImage.style.display = “none”;
return true;
}
}
url = “../cgi-bin/sendmessage.cgi?username=” + user_mobile + “&pass=” + mycosmosPsw + “&phone=” + mobile + “&mes=” + sms_text;
xmlhttp.open(“GET”,url,true);
xmlhttp.send(null);[/code]

but when the same request is made synchronously everything works..

[code=php]var loaderImage = document.getElementById(“loader”);
loaderImage.style.display = “inline”;
xmlhttp = new XMLHttpRequest();
url = “../cgi-bin/sendmessage.cgi?username=” + user_mobile + “&pass=” + mycosmosPsw + “&phone=” + mobile + “&mes=” + sms_text;
xmlhttp.open(“GET”,url,false);
xmlhttp.send(null);
loaderImage.style.display = “none”;
response = xmlhttp.responseText;
alert(response);
return true;[/code]

what shall I do to make the asynchronous code to work?

Should it be noted, the cgi file takes 2-4 seconds to load.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineAug 27.2010 — Under what protocol are you running? If you're running locally using [FONT="Courier New"]file:[/FONT], then [FONT="Courier New"]request.status[/FONT] will be zero. If so change the test to this:
[CODE]if( this.readyState == 4 && ( /^http/.test( location.protocol ) ? this.status == 200 : true) )
{
...
} [/CODE]
Copy linkTweet thisAlerts:
@bzztauthorAug 27.2010 — Sorry I forgot to mention that I was aware that status is sometimes(?) 0 when you call local scripts so I did put the cgi script to another server and it didn't work either.

As far as the "using [FONT="Courier New"]file[/FONT]:" I don't really understand what you mean. What I do, is make a call to a CGI script.

Should I try the code you suggested as is?
Copy linkTweet thisAlerts:
@A1ien51Aug 27.2010 — I do not think the problem is with the JavaScript call, but with the way you are calling the function. Are you calling it with a button click? If you are make sure to return false on the click of the button.

Eric
Copy linkTweet thisAlerts:
@aj_nscAug 27.2010 — What if you called open() before you set your onreadystatechange listener? I seem to recall coming across something about the order of these arguments being important to something a while back....while logically it looks like it shouldn't make a difference, there was some reason for it I can remember.

Hey, you never know.

EDIT: Bingo! (at least for IE)

Link -> http://msdn.microsoft.com/en-us/library/ms535874(VS.85).aspx

On IE, XMLHttpRequest object properties are reset when you call the open method. The onreadystatechange handler you just set on the line above is thrown away. Just switch the order of those two lines.

TO RECAP: Calling the open method initializes the request object. If you have set properties (such as onreadystatechange) it is reset to initial default values (NULL). So, do all your initialization between OPEN and the final call to SEND.
[/quote]
Copy linkTweet thisAlerts:
@bzztauthorAug 27.2010 — I do not think the problem is with the JavaScript call, but with the way you are calling the function. Are you calling it with a button click? If you are make sure to return false on the click of the button.

Eric[/QUOTE]

[B][COLOR="Red"]SOLVED![/COLOR][/B]

why was that the problem???

Now I;ll have to find a way to pass the button click to the PHP itself because it also does some things but that's no problem. I'll handle it. Thanks!

It was the first time that I had a click to be processed by javascript AND by PHP itself at the same time, so it was the first time I used AJAX without returning false...
Copy linkTweet thisAlerts:
@A1ien51Aug 27.2010 — Why would you have to process it in two paths? Seems like a bad design to me.

Eric
Copy linkTweet thisAlerts:
@bzztauthorAug 27.2010 — Why would you have to process it in two paths? Seems like a bad design to me.

Eric [/QUOTE]

You should be right, it most probably is...

What I do is use an external site to send a sms using the CGI script (PERL/LWP), and PHP to save the text of the sms to the user's send messages (PHP/SQL). The whole site is in PHP/MySQL but I used PERL because I don't know some way for PHP to log on securely to a site and post some data afterwards. Having used PERL lots of times mostly for such kinds of tasks I didn't think it twice
×

Success!

Help @bzzt spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...