SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Temecula, CA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple Ajax Requests

    I'm hoping that this is a good place to post an Ajax question.

    I would like to make multiple Ajax requests with the onload script of a page. So far I can only get one to work. It doesn't matter which one I comment out, the other one works, just not both. Can anyone tell me why and/or what to do about it?

    Code:
    	function GetAjaxContent()
       	{
       		var request = false;
       		request = CreateRequest();
       
       		CallXMLhttpRequest(request, 'SideNav.php', 'SideNav');
       
       		//var request2 = false;
       		//request2 = CreateRequest();
       
       		//CallXMLhttpRequest(request2, 'Content.php', 'Content');
       	}
       
       In another file:
       function CreateRequest()
       {
       	try {
       	  request = new XMLHttpRequest();
       	} catch (trymicrosoft) {
       	  try {
       		request = new ActiveXObject("Msxml2.XMLHTTP");
       	  } catch (othermicrosoft) {
       		try {
       		  request = new ActiveXObject("Microsoft.XMLHTTP");
       		} catch (failed) {
       		  request = false;
       		}
       	  }
       	}
       
       	if (!request)
       	  alert("Error initializing XMLHttpRequest! This browser is not compatible with Ajax!\nPlease download and use the latest version of Internet Explorer or FireFox.");
       	else
       		return request;
       
       }
       
       function CallXMLhttpRequest(request, Content, Target, InnerTarget, Popup)
       {
       	if (Popup)
       	{
       		AjaxPopup = true;
       	}
       		AjaxInnerTarget = InnerTarget;
       	
       	var url = Content;
       	AjaxTarget = Target;
       	request.open("Get", url, true);
       	request.onreadystatechange = updatePage;
       	request.send(null);
       }
       
       function updatePage()
       {
       	if (request.readyState == 4)
       	{
       		if (request.status == 200)
       		{
       			var response = request.responseText;
       			if (AjaxPopup)
       			{
       			    
       				response += &quot;<p class='CloseLink'>Close
       
       &quot;;
       				document.getElementById(AjaxTarget).innerHTML = response;
       				showBlock(AjaxTarget);
       			}
       			else
       			{
       				document.getElementById(AjaxTarget).innerHTML = response;
       			}
       
       			if (AjaxInnerTarget.length > 0)
       			{
       				window.location.href=AjaxInnerTarget;
       			}
       		}
       		else
       			alert(&quot;status is &quot; + request.status);
       	}
       }

  2. #2
    SitePoint Addict richtestani's Avatar
    Join Date
    Nov 2003
    Location
    Bridgeport
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems even though you are making 2 requests, you are creating the same named object 'request'.

    I think you need to make 2 objects

    req1 = new xmlHttpRequest();
    req2 = new xmlHttpRequest();
    RichTestani
    -------------------------------
    http://www.junkdepot.com
    http://www.rareoopdvds.com | The Movie Poster Site

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Temecula, CA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, what you're suggesting would solve the problem, I think; but what I'm trying to do is create a mechanism by which any number of Ajax calls can be made.

    I think the crux of my trouble lies in the javascript object model or my lack of understanding of it. I was looking at it this way:
    1. I ask CreateRequest to return an object from XMLHttpRequest()
    2. I pass that object to CallXMLhttpRequest wherein the requisite properties are defined and the request is sent.
    3. When all is well and good, the XMLHttpRequest() object calls its onreadystatechange event handler, in this case 'updatePage', and the results are returned.
    The problem is that even when I set up the code such that it succeeds with the steps above, (I added "var request;" to the CreateRequest function to ensure that the request object was local to the function) it believes 'request' to be undefined within the onreadystatechange function, 'updatePage'.

    I thought that updatePage would be called from within the context of each request object, as one of the XMLHttpRequest() object event handler functions.

    I've read that onreadystatechange is one of the request objects events; so how do I represent the object itself within the event handler? I tried using 'this.readyState', etc., but the results were not any better.

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •