gareth
gareth

Reputation: 189

jquery doing two ajax calls under on function

if have the following two scripts, when a button is clicked i wish for them both to be loaded, at the moment they are conflicting? any suggestions? the first needs to run before the second. I have tried calling two separate function but still i get a conflict

function showUser3(str)
{


if (str=="")
  {
  document.getElementById("basketShow").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("basketShow").innerHTML=xmlhttp.responseText;
    }
  }

        var Id = str;

        var qty = $("#"+Id).find("#qty").val();
        var productID = $("#"+Id).find("#productID").val();
        var categoryID = $("#"+Id).find("#categoryID").val();
        var priceID = $("#"+Id).find("#priceID").val();

        var url = 'ajaxAddBasket.php?productID='+productID+'&categoryID='+categoryID+'&qty='+qty+'&priceID='+priceID+'&Id='+Id;

xmlhttp.open("GET",url,true);
xmlhttp.send();


if (str=="")
  {
  document.getElementById("ajaxPallet").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("ajaxPallet").innerHTML=xmlhttp.responseText;
    }
  }


var url = 'ajaxPallet.php';
xmlhttp.open("GET",url,true);
xmlhttp.send();

}

Upvotes: 0

Views: 87

Answers (2)

Felix Kling
Felix Kling

Reputation: 816422

If you convert your Ajax calls to jQuery's ajax [docs] method, you can make use of deferred objects [docs] and pipe [docs] to chain the calls:

$.ajax({...}).pipe(function() {
   return $.ajax({...});
});

Have a look at the documentation for more examples.

Upvotes: 1

Stijn_d
Stijn_d

Reputation: 1078

I would just us the basic $.ajax function of jquery. And on the complete/succes event of the first call the second one with $.ajax?

Check this link out: http://api.jquery.com/jQuery.ajax/

Upvotes: 0

Related Questions