Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Simple Ajax "HTTP POST" - Internet Explorer hangs... again

Posted on 2008-10-30
8
2,106 Views
Last Modified: 2013-12-08
Hi everyone,

I'm having some problems with Ajax and HTTP POST.

It's a simple script. a hyperlink with a onclick event, will trigger an AJAX POST.

On IE, if you click it a few times, it will simply hang. As if there are no response from the page.

Other browsers, it is perfectly fine.

This is really an irritating issue.

Anyone can give some sound advise?


function positive(userid,ad)
{
				var xmlHttp;
				var returnedtext;
				try{xmlHttp=new XMLHttpRequest();}
				catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
				var strTS = new Date().toString().replace(/(\s|\:)/gi,"");	
				var url = "/rate.php";
				var params =   'sellerid=' + userid
							 + '&adid2=' + ad
							 + '&comment=' + document.getElementById('txacomments').value
							 + '&ratetype=POSITIVE'
							 + '&t=' + strTS;
							 
				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.onreadystatechange = function() 
				{//Call a function when the state changes.
					if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
					{
						var response = xmlHttp.responseText;
						if(response == 'OK')
						document.getElementById('answerbox').innerHTML = '<strong>Your rating was submitted successfully</strong>';
					};
				};
				xmlHttp.send(params);
}
 
 
 
 
 
on rate.php, just a p-code to demostrate
 
<?php
echo "OK";
?>

Open in new window

0
Comment
Question by:stevong
  • 4
  • 4
8 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 200 total points
ID: 22842656
try this (copy and paste):
 var xmlHttp=null;
 var q=Array();
function positive(userid,ad)
{ 
	if( xmlHttp ){
		q[q.length]=userid;
		q[q.length]=ad;
		return false;
	}
                       
                        var returnedtext;
                        try{xmlHttp=new XMLHttpRequest();}
                        catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
                        var strTS = new Date().toString().replace(/(\s|\:)/gi,"");      
                        var url = "/rate.php";
                        var params =   'sellerid=' + userid
                                           + '&adid2=' + ad
                                           + '&comment=' + encodeURIComponent(document.getElementById('txacomments').value)
                                           + '&ratetype=POSITIVE'
                                           + '&t=' + encodeURIComponent(strTS);
                                           
                        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.onreadystatechange = function() 
                        {//Call a function when the state changes.
                              if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
                              {
                                    var response = xmlHttp.responseText;
                                    if(response == 'OK')
                                    document.getElementById('answerbox').innerHTML = '<strong>Your rating was submitted successfully</strong>';
							 
							 xmlHttp=null;
							 if(q.length)
							 {
							 	a=q.shift();
								b=q.shift();
								positive(a,b);
							 }
                              };
                        };
                        xmlHttp.send(params);
}

Open in new window

0
 

Author Comment

by:stevong
ID: 22842707
Thanks hielo!

What's the reason/rationale behind the proposed script?

0
 
LVL 82

Expert Comment

by:hielo
ID: 22843016
the one thing that stands out as wrong from what you posted is that you are not encoding the data you are posting. Notice how I used encodeURIComponent on the parameter values. Try that first on your original post. Other than that, what I posted is basically doing attempting to do synchronous request, but without "freezing" the browser.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:stevong
ID: 22846912
Thanks!

What about the shift() and positive() ? and

            q[q.length]=userid;
            q[q.length]=ad;
            return false;


What's the rationale?

THanks
0
 
LVL 82

Expert Comment

by:hielo
ID: 22846929
>>What about the shift()
Your function name is positive(). When you call positive the first time, a request will be made immediately. If you then click immediately before the previous request completes, the values are saved in the q array. So if you click the link consecutively 3 times, one request will be made immediately, the other two will be "pending". So on the second click, the parameters are added at the end of the array. When you click a third time, the parameters for that click are also added at the end of the array. The point is that in the array, the values infront of the array are the parameters of the early requests, so those should be "serviced" first. The shift() removes the items from the front of the array.

>>What about the positive() ?
When the previous request completes, I am simply checking if the q array contains any items (which equates to having "pending requests") and if so, I call positive() by passing the items from the front of the array first.
0
 

Author Closing Comment

by:stevong
ID: 31511724
Thanks!
0
 

Author Comment

by:stevong
ID: 22848102
Anyway, may I ask:

Why is xmlHttp assigned as a global variable instead?
0
 
LVL 82

Expert Comment

by:hielo
ID: 22855648
>>Why is xmlHttp assigned as a global variable instead?
If you do not do so, you won't know if there is request in progress.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question