Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ajax - crashing IE

Status
Not open for further replies.
Mar 3, 2006
25
My Ajax routine runs a dozen times (or so) with no problem and then stops all internet connections. Javascript is still working since I have some js popup windows that still activate but my ajax routine (saving data to the server) stops working and I can't post/get a form to the server. I read something about multiple ajax requests per page causing a race condition (or something like that) if I'm using global variables instead of local. I think I've got all local variables now but I'm still having the problem. Maybe one of you can tell me what I'm doing wrong. Doesn't seem to be a problem in Firefox.

ajaxRequest(f,pairs,func) starts the routine. f is the form, pairs I leave blank usually(an in this case specifically) so that getFormValues() will just send all values of the form to the server, and func is the PHP function I want the server to run on the current form data. PHP may or may not send anything back.

Code:
function ajaxRequest(f,pairs,func) {
	var file = '/index.php';
	var SID, str;
	var http_request = false;
	if(func != "") {
		func = "dsup="+func+"();&";
	}
	if(pairs=="") {
		str = "ajax=true&"+func+getFormValues(f,"");
	} else {
		if(sid) {
			SID = sid+'&'
		} else if (document.getElementById('PHPSESSID')) {
			SID = "PHPSESSID="+document.getElementById('PHPSESSID').value+"&";
		} 
		str = "ajax=true&"+SID+func+pairs;
	}
	if (typeof window.ActiveXObject != 'undefined' ) {
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		http_request = new XMLHttpRequest();
	}
	http_request.open( "POST", file, true );
	http_request.setRequestHeader("Content-Type","application/x-[URL unfurl="true"]www-form-urlencoded;[/URL] charset=UTF-8");
	http_request.onreadystatechange = function() { if (window.ajaxUpdate)ajaxUpdate(http_request); };
	http_request.send(str);

}
function ajaxUpdate(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var response = http_request.responseText.split("|");
			if (response[0]!="") {
				eval(response[0]);
			}
		} else {
			alert("Status code " + http_request.status+"\n"+
			"Server connection appears to be\n"+
			"down.  Try re-entering your request\n"+
			"or try again later.");
		}
	}
}
function getFormValues(fobj,valFunc) {
	var str = "";
	var valueArr = null;
	var val = "";
	var cmd = "";
	for(var i = 0;i < fobj.elements.length;i++) {
		switch(fobj.elements[i].type) {
			case "text":
			if(valFunc) {
				cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
				val = eval(cmd)
			}
			str += fobj.elements[i].name +
			"=" + escape(fobj.elements[i].value) + "&";
			break;
			case "checkbox":
			if(fobj.elements[i].checked) {
			str += fobj.elements[i].name +
			"=" + escape(fobj.elements[i].value) + "&";}
			break;
			case "hidden":
			if(valFunc) {
				cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
				val = eval(cmd)
			}
			if(fobj.elements[i].name!='dsup'){
			str += fobj.elements[i].name +
			"=" + escape(fobj.elements[i].value) + "&";}
			break;
			case "select-one":
			str += fobj.elements[i].name +
			"=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
			break;
		}
	}
	str = str.substr(0,(str.length - 1));
	return str;
}


Tusan Tuk
Rois
 
I forgot to mention I'm calling it usually like this:

<button onclick="ajaxRequest(document.forms[0],'','ajax_ee_tc_sub');return false;">gogo</button>

Rois
 
just one thing, does the PHP page call itself inside the code?

on the face of it your code looks ok. will check it and let you know...

Known is handfull, Unknown is worldfull
 
PHP is not in these scripts. This is an external script file.

I'd have to admit I'm still kinduva js novice and I'm probably doing way more complicated stuff than I ought but but that seems to be the way I learn best.

I'm wondering if calling the routine the way I do
Code:
ajaxRequest(document.forms[0],'','ajax_ee_tc_sub');

instead of doing something like
Code:
win[x] = new ajaxRequest(document.forms[0],'','ajax_ee_tc_sub');
x++;

might be my problem. Could it be that NOT creating a new object for each request might be causing my problem? Just trying to throw some ideas out.

This child page (opened with window.open from it's parent) has a minimum of 1 ajax request (downloading previous data for a specifi date) and potentially as many as 20 ajax requests(saving data for the same date.) The parent page may open 1 to 21 different child pages before closing but there are no ajax requests on the parent page.

Any insight would be greatly appreciated.
Rois
 
hmm, the first thing that we need to check is wether this is a single browser issue. did the same happen on other systems?

if yes, then i guess like you pointed out it could be the multiple requests that are happening at the same time.

i guess to find that we nned to create a test script. one that calls XMLHTTP object directly, rather tha your function about 30 times (using a for loop)...

Known is handfull, Unknown is worldfull
 
I added the 'new' object

win[x] = new ajaxRequest(document.forms[0],'','ajax_ee_tc_sub');
x++;

but I'm still having the same issue and yes it is doing the same thing on 2 completely different machines with XP IE.

I'll give your test script idea a try and see if I have the same problem.

Thanx
Rois
 
Well, I setup a test script to send numbers 1 to n to the server and have the server send back a js to put that number in the innerHTML of an anchor. Worked perfectly. Even 1 to 1000.

I think I figured out what the problem was. Most of my ajax requests are done before I close the window making the request. HOWEVER, part of my test pattern includes changing a text input and clicking the close button(window.close();) before leaving that text box. The text box has an onchange trigger to call ajax and save the text. Nothing is being returned by the server from the text box change BUT previously I had a button showing that an ajax routine was running and I returned some javascript to hide the active ajax button. I had recently discontinued that button routine but only by remarking it out of returned stuff (print "/*hideButton();*/"; from PHP) instead of deleting it altogether. Apparently a remarked out statement to a closed window was hanging all communications to the webserver. I deleted the PHP code so nothing is being returned to ajax and IE stopped hanging.

That would have been impossible for anyone on the forum to know about since it didn't occur to me that my PHP program (that the ajax request called) might be causing my problems. That's a tough call when one programming language is interacting with another through the webserver.

Anyway, thanks for the input.

Rois
 
welcome...

Known is handfull, Unknown is worldfull
 
I took a look at prototype and it looks good. I do try to us pre-done stuff when it works for me but I'm still learning JS so the experience of writing my own is help me learn more.

Thanx
Rois
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top