Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, I am developing a JSP application, in which I need to update two more drop down lists dynamically using ajax. For this I wrote two different JS Functions which creates AJAX request to two different URLs. They are working good when I called any one of those two functions with onChange of select control. But failed when I placed both of them at a time. Please help me how I can call two JS functions, which sends request to two different URLs concurrently.
Thank you in advance.
Posted
Updated 24-Apr-14 14:41pm
v2

I'll just take a guess since there wasn't much detail in your post. It sounds like one of the ajax calls (or both) is relying on values set by the other. If this is the case then what you are expecting is them to run in the order you called them.

For instance you are expecting

JavaScript
function(){
    a();
    b();
}


What happens is a() gets called, and then without waiting for it to finish b() gets called. If a() is setting a value that you need, then b() will fail. You can turn the async functionality off.

For jquery you could do:

$.ajax({
......
url: '...',
data: dataStuff,
async:false,
........
});


and then b() would not run until a() is finished.
 
Share this answer
 
Actually I am not using jQuery in my application. And another thing is I am using the same reference variable for XMLHttpObject by thinking as soon after completion of of JS function, the local variables will expire, but it didn't happened. I experimented with changing the reference variable, it worked good.
Thankyou all for responding my problem.
 
Share this answer
 
Without seeing how your code is setup to function at the moment, one can only guess.
If you have a look at the AJAX tutorials at wschools.com (http://www.w3schools.com/ajax/ajax_examples.asp[^] - particularly: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_callback[^] and http://www.w3schools.com/ajax/ajax_xmlfile.asp[^])

You can see that they use a single function to create the XMLHttpRequest object. When put into a better function than the example they give (which is flawed for me, since they only pass the .responseText to the callback function, rather than the XMLHttpRequest object itself)


I use the following function for GET requests:

JavaScript
function myAjaxRequest(url, callback)
{
    var ajax = new XMLHttpRequest();
    ajax.onreadystatechange = function()
    {
        if (this.readyState==4 && this.status==200)
            callback(this);
    }
    ajax.onerror = function()
    {
        console.log("AJAX request failed to: " + url);
    }
    ajax.open("GET", url, true);
    ajax.send();
}


Which I then pair with a callback function as required. If you look at their XML example, you'll see that you need to process the .responseXML
On the other hand, if you're grabbing normal text, you want to deal with the .responseText

I've just tried the following code (loadXMLDoc2 is called in response to a button click)
as a simple test of sending out a pair of requests at the same time, seems fine. I've always used AjaxToolbox[^] in the past, but rarely need the extra goodies it provides - I'm better off typing the ajax code out into my source, for the savings in size. (AjaxToolbox is 5kb) Much of the goodies are related to form POST handling and to maintain a list of current pending requests, if memory serves me correctly.

JavaScript
function loadXMLDoc2()
{
    myAjaxRequest('cd_catalog.xml', onCdXmlLoaded);
    myAjaxRequest('cd_catalog.xml', onCdXmlLoaded2);
}
function onCdXmlLoaded(ajax)
{
    var xmlDoc = ajax.responseXML;
    var txt ='';

    x=xmlDoc.getElementsByTagName("ARTIST");
    for (i=0;i<x.length;i++)
    {
      txt=txt + x[i].childNodes[0].nodeValue + "<br>";
    }
    document.getElementById("myDiv").innerHTML=txt;
}

function onCdXmlLoaded2(ajax)
{
    var xmlDoc = ajax.responseXML;
    var txt ='';

    x=xmlDoc.getElementsByTagName("TITLE");
    for (i=0;i<x.length;i++)
    {
      txt=txt + x[i].childNodes[0].nodeValue + "<br>";
    }
    document.getElementById("myDiv2").innerHTML=txt;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900