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 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.