Jump to content

Why am I getting weird results in my Ajax call ? Im so frustrated!!!


2levelsabove

Recommended Posts

The function below has 2 alert boxes. When I run it, sometimes the outer alert box comes empty. Please explain in a human manner why .

 

 


function ItemCount()//returns the number of items in carousel
{


if (window.XMLHttpRequest) {
		var	req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		var req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
	alert("upgrade browser");

	}


	req.open("POST", "/includes/_process/getPortfolioCount.php", true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.send(null);
	req.onreadystatechange = function() {
		if ((req.readyState == 4) && (req.status == 200)) {


			if (req.responseText.length) {


			document.getElementById('portfolioCount').value=req.responseText;

			alert(req.responseText);

			}

		}
	}

alert("out:"+document.getElementById('portfolioCount').value);




return document.getElementById('portfolioCount').value;

}//function 

That's exactly it.

 

On a side note:

 

req.send(null);

 

If you are not actually sending any post data (which you aren't, since you are sending NULL), then it's better to make your AJAX call a 'get' call instead of a 'post' call.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.