Nesting AJAX

S

steve.chambers

Is it possible to do this? Does AJAX allow one call to be made within
another?? So far I'm having problems. Below I've tried to simplify my
code into its basic elements - the problem I'm getting is that that
ResponseHandler1 is getting called fine from an intial call to
NestedAjaxCalls but ResponseHandler2 (the nested call) isn't. I've
tested out this very code to make sure!

function NestedAjaxCalls() {
var ajax_connection = createRequest();
ajax_connection.open('get', 'request1.php');
ajax_connection.onreadystatechange = function() {
ResponseHandler1(ajax_connection);
}
ajax_connection.send(null);
}

function ResponseHandler1(ajax_connection) {
var ajax_connection2 = createRequest();
ajax_connection2.open('get', 'request2.php');
ajax_connection2.onreadystatechange = function() {
ResponseHandler2(ajax_connection);
}
}

function ResponseHandler2(ajax_connection) {
// Does not get called
echo('Success! Nested AJAX call was made successfully.');
}

// create an AJAX Request
function createRequest() {
var ajaxRequest;

try
{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e1)
{
try
{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
ajaxRequest = new XMLHttpRequest();
}
}

return ajaxRequest;
}



Is it something simple I'm doing wrong like the scope of my connection
variables or is this a limitation with AJAX - if so could anyone
suggest an alternative method of doing the same thing? Any replies
gratefully received...

Cheers,
Steve
 
L

-Lost

Is it possible to do this? Does AJAX allow one call to be made within
another?? So far I'm having problems. Below I've tried to simplify my
code into its basic elements - the problem I'm getting is that that
ResponseHandler1 is getting called fine from an intial call to
NestedAjaxCalls but ResponseHandler2 (the nested call) isn't. I've
tested out this very code to make sure!
Is it something simple I'm doing wrong like the scope of my connection
variables or is this a limitation with AJAX - if so could anyone
suggest an alternative method of doing the same thing? Any replies
gratefully received...

I would suggest you take a look at Matt Kruse's http://www.ajaxtoolbox.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top