当前位置: 动力学知识库 > 问答 > 编程问答 >

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();

}

网友答案:

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.

网友答案:

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/

分享给朋友:
您可能感兴趣的文章:
随机阅读: