Jump to content

Multiple ajax requests


x1nick

Recommended Posts

Im still learning javascript at the moment, so bare with me on this.

 

I need to make 2 ajax requests when a user presses a button, which update two separate divs.

 

My ajax file (any constructive opinions on my class would be appreciated too! don't know how good it actually is)

function ajaxclass () {
  var xmlhttp;
  this.request = request;

  function GetXmlHttpObject() {
    if (window.XMLHttpRequest)   {
      return new XMLHttpRequest();
    }
    if (window.ActiveXObject("Msxml2.XMLHTTP")) {
      return new ActiveXObject("Msxml2.XMLHTTP");
    }
    if (window.ActiveXObject("Microsoft.XMLHTTP")) {
      return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
  }  
  
  function request(url,loaded,loading) {    
    xmlhttp = GetXmlHttpObject();
    if (xmlhttp == null) {
      alert ("Browser does not support HTTP Request");
      return;
    }  
    xmlhttp.onreadystatechange = function() {
      switch(xmlhttp.readyState) {
        case 0: // Uninitialized
          break;
        case 1: // Loading
          if (loading != "") {
            eval(loading);
          }
          break;
        case 2: // Loaded
          break;
        case 3: // Interactive
          break;
        case 4: // Done!
          response = xmlhttp.responseText;
          eval(loaded);
	  break;
        default:
          break;
      }  
    }
    xmlhttp.open("GET",url,true);  
    xmlhttp.send(null);   
  }
  
}

var ajax = new ajaxclass();

 

It does work, but I have a problem when carrying out 2 requests.

The button runs this function

 

<script type="text/javascript">
  function doupdate (tplid) {
    var result = confirm('Are you sure you want to update {tplname}?');
    if (result) {
      openmessage();
      ajax.request('index.php?s=ajax&acms&amod=tpl&ajax=update&tplid='+tplid, 'document.getElementById(\'messagearea\').innerHTML = response;','');
      ajax.request('index.php?s=ajax&acms&amod=tpl&ajax=results', 'document.getElementById(\'filterreslts\').innerHTML = response;','');
    }
  }
</script>

 

Now firebug says both run, and both scripts return a response. The 2nd ajax request dispalys a result, but the first dosen't.

If the 2nd request is removed the first one works, and displays the message in the correct area.

 

Why is it when both are run, that the message is not displayed but firebug reports it as returning the required result?

 

PS openmessage() simply sets a div to display.style = 'block' which works, but the div is empty.

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.