2012-04-04 97 views
0

首先我加载PHP页面然后从JAVA脚本调用第二个PHP和张贴一些变量和操作功能一个... Java函数调用首先AJAX请求是指第一个AJAX请求仍然是工作......AJAX请求

现在在调用其他JAVA脚本,创建另一个AJAX请求其称之为第三PHP其生成地图API XML数据的第二个PHP函数一个...意味着第二AJAX request..but,因为第一个请求仍然没有失败完成...我该如何处理?这AJAX调用PHP和PHP的呼叫第二AJAX请求?...

请帮我....

看我的要求,按您的例子,因为我的代码太长,张贴和格式这里每堆栈格式

首先,我在浏览器直接加载ajax1.php。

现在我致电新AJAX请求

$.ajax('/ajax2.php', function(data2){ 
// But the ajax2.php having Call another AJAX request for ajax3.php which retrive XML Data genereted from mysql and create Google MAP 
    $.ajax('/ajax3.php', function(data3){ 
     // I need to display the ajax2.php Generated MAP to DIV created by ajax1.php 
    // Whene I call ajax2.php directy from browser url it AJAX ajax3.php runs and gerete MAP perfectly 
    // But I call ajax2.php through ajax1.php AJAX request it dosen't create the ajax3.php AJAX request 
    }); 
}); 

总之我致电新AJAX请求,但首先AJAX请求调用另一个二AJAX请求。它生成MAP。

document.getElementById("ntfs").innerHTML=xmlhttp.responseText; 
// This above code is not working but I write like 

document.write(xmlhttp.responseText); 
//This above Code works perfect, but it create new document I can't fill to DIV.innerHTML 

为什么?发生这种事?

回答

0

一如往常,这将是很容易帮助你,如果你贴一些自己的代码。我将使用jQuery。

你要明白,AJAX是异步的。 IDE不会等待ajax调用完成才能执行下一行代码。如果你想有ajax1呼叫ajax2当它完成,并ajax2调用ajax3其完成时,你需要利用你的Ajax技术的finished:complete:处理程序。 jQuery中它应该是这样的......

$.ajax('/ajax1.php', function(data1){ 
    $.ajax('/ajax2.php', function(data2){ 
     $.ajax('/ajax3.php', function(data3){ 
      //All 3 ajax calls are done, in order 
     }); 
    }); 
}); 

alert('the first ajax call was made and is probably still executing'); 

如果,例如,你想ajax1和ajax2在同一时间运行,并有ajax2呼叫ajax3其完成时,它看起来像这样.. 。

$.ajax('/ajax1.php', function(data1){ 
    //ajax1 call is done 
}); 

$.ajax('/ajax2.php', function(data2){ 
    //ajax2 is done 
    $.ajax('/ajax3.php', function(data3){ 
     //ajax2 and ajax3 calls are done, in order 
    }); 
}); 

alert('the first 2 ajax call were made and are probably still executing'); 

补充阅读: Asynchronous vs. Synchronous