hi,
basically i have this function.

function showDetail(checkBoxShipmentId)
{
	xmlhttp = GetXmlHttpObject();   // this calls function below 
	if (xmlhttp==null)
  	{
  		alert ("Browser does not support HTTP Request");
  		return;
  	}
	
	var url="detailBackend.php";
	url=url+"?shipmentid="+checkBoxShipmentId;    //?shipmentid = value of the checkbox, which is the shipmentid
	url=url+"&ran="+Math.random();
	if(xmlhttp.readyState ==4)
	{
		xmlhttp.onreadystatechange= displayDetail;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	
	else
	{
		status = xmlhttp.readyState;
		alert(status);       ///// this always return 0 ///// 
	}
	
}

which calls this function to initialize xmlhttp

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
  	{
  		// code for IE7+, Firefox, Chrome, Opera, Safari
  		return new XMLHttpRequest();
  	}

	if (window.ActiveXObject)
  	{
  		// code for IE6, IE5
  		return new ActiveXObject("Microsoft.XMLHTTP");
  	}
	return null;
}

/******** i also have this function which is working *******/

var xmlhttp;

function showShipment(shipmentIdFromDropDown)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
  	{
  		alert ("Browser does not support HTTP Request");
  		return;
  	}
	var url="responsexml.php";
	url=url+"?q="+shipmentIdFromDropDown;
	url=url+"&ran="+Math.random();     //generate a random number and store to variable ran to make sure cookie or session don't make them all the same
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

Q: i am wondering if I have 2 functions calling the GetXmlHttpObject() function,,,, would that cause conflict or something? I really don't think it should.

Thanks for helping AJax newbie...

k2k,

The two calls to GetXmlHttpObject() will each cause an independent request objets to be returned and they should both work quite happily.

The only proviso is that if the two response handlers both affect the same js objects or the same areas of the DOM, then you could get "race effects", whereby two closely dispatched requests could respond in the reverse order and yield unexpected results.

However, in general this won't be an issue.

Airshow

Other than all the obvious errors on the client side, the main reason for this is that gecko engine looks for the Access-Control-Allow-Origin in the header from the servlet. If it does not find it, it will abort the communication and you get a status=0 and statusText=null. Also, the moz-nullprincipal in xml parsing error. All of this stuff is very misleading. All you need to resolve this problem is:

response.setHeader("Access-Control-Allow-Origin","*");

In the servlet code and life will be good :-)

MozAndy,

This discussion probably belongs elsewhere but here goes .....

It's probably me but I'm struggling to see the relevance of your answer (interesting though it is) to K2K's question.

  • Question is about multiple ajax requests.
  • Access-Control-Allow-Origin header is about cross-domain (cross-origin) issues.

Given that K2K's code uses relative urls, surely same-domain (same-origin) must apply.

Hence, an Access-Control-Allow-Origin header appears not to be necessary, and in any case irrelevant to solving issues with multiple simulatneous ajax requests.

What am I missing?

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.