/    Sign up×
Community /Pin to ProfileBookmark

Inconsistent AJAX Response in IE

Hello,

I’m using AJAX in a login form for user authentication. The Javascript code is shown below:

[CODE]function login_check(email, password){
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
else
{
alert(“Your browser does not support XMLHTTP (which is required to use this site), or your security setting is too high. All modern browsers support XMLHTTP. Please upgrade your browser to a newer version or lower your security setting to use this site.”);
}

// define request response
var request_response;
xmlhttp.onreadystatechange=function()
{
try {
if(xmlhttp.readyState==4)
{
if(xmlhttp.status == 200) {
request_response=xmlhttp.responseText;
<do a bunch of stuff here>
}
else
{
alert(‘There was a problem with the XMLHTTP request.’);
}
}
}
catch ( e ) {
alert(‘Caught Exception: ‘ + e.description);
}
}

// send request to server
var url=”PHPscripts/login_check.php”;
var params=”email=”+email;
params=params+”&password=”+password;

xmlhttp.open(“POST”,url,true);

//Send the proper header information along with the request
xmlhttp.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
xmlhttp.setRequestHeader(“Content-length”, params.length);
xmlhttp.setRequestHeader(“Connection”, “close”);
xmlhttp.send(params);
}[/CODE]

As mentioned earlier, I get inconsistent behavior in IE (behavior is fine in Firefox). In IE, even for the same email and password (the only 2 arguments passed into the Javascript function above), I get one of three behaviors:
-sometimes login is succesful
-sometimes I get the ‘There was a problem with the XMLHTTP request.’ message shown in the code above (indicating that status != 200)
-and sometimes nothing happens when the login button is pressed.

Again, these inconsistencies in IE occur even between different trials of using the same login email and password. Does anyone know why this may occur, and how it could be fixed?

EDIT: Fogot to mention that I am using IE7

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@KorJun 23.2009 — IE keeps the server-side page in cache. You need either to instruct the php file to send a no-cache header or to send a random value to the server-side application in order to force a reloading from server. Second variant is easier. Try this:

<i>
</i>...
// send request to server
var url="PHPscripts/login_check.php";
var params="email="+email;
params=params+"&amp;password="+password;

[COLOR="Blue"]var nocache = Math.random();//fix for IE who keeps the requested file in cache
params+="&amp;nocache="+nocache;
[/COLOR]
xmlhttp.open("POST",url,true);
...


Now, if in the php file the "nocache" element of the request's array does bother you, you may get rid of it from the very beginning using the php method [B]unset()[/B]
Copy linkTweet thisAlerts:
@ed423authorJun 23.2009 — Thanks Kor!
Copy linkTweet thisAlerts:
@ed423authorJun 23.2009 — Actually, I implemented your suggestion in order to bypass the cache, and I still get the same issues. Even with the same email or password, I will get one of three behaviors:

1. Nothing happens

2. I am returned the 'There was a problem with the XMLHTTP request.' message shown in the code in my original post

3. Login is successful (the expected behavior)

For Cases 1 and 2 above, I did some debugging and found out that:

For Case 1, the [B]readyState[/B] property of [B]XMLHttpRequest[/B] stays at 1, indicating that "the request has been set up", and the php script never even begins to run (I tested this by setting some session variables in the php script and then checking theses variables to see if they have been set).

For Case 2, the [B]readyState[/B] property of [B]XMLHttpRequest[/B] stays does become 4, indicating that "the request is complete", but the [B]status[/B] property is not 200 (200 means that the request is successful). Like Case 1, the php script never even begins to run.
Copy linkTweet thisAlerts:
@riddlaJun 23.2009 — if your using ajax for anything else on the page make sure that your ajax calls are not colliding...
Copy linkTweet thisAlerts:
@ed423authorJun 24.2009 — I don't think I have any colliding AJAX calls. Is there a way I can check this?
Copy linkTweet thisAlerts:
@KorJun 25.2009 — I don't think I have any colliding AJAX calls. Is there a way I can check this?[/QUOTE]
Use Firefox and its addon called Firebug, which will show you on its console the AJAX calls

https://addons.mozilla.org/en-US/firefox/addons/versions/1843
×

Success!

Help @ed423 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 5.2,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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