Javascript.在当前AJAX停止后运行AJAX流
Javascript. Run a AJAX flow after current AJAX stops
所以我有一个简单的javascript,当用户单击more时,它可以从数据库加载更多的注释。现在,我想扩展这个脚本,以便它在开始允许用户查看注释之前首先填充SQL数据库。我觉得我走在了正确的轨道上,但我无法让它发挥作用。
首先是执行WORK的代码。
$(function() {
$('.load_more').live("click",function() {
var photoid = document.getElementById('photoid').value;
var lastid = document.getElementById('lastid').value;
if(lastid!='end'){
$.ajax({
type: "POST",
url: "/more_comments_ajax.php",
data: {
photoid : photoid,
lastid : lastid
},
beforeSend: function() {
$('a.load_more').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request
},
success: function(html){//html = the server response html code
$("#more").remove();//Remove the div with id=more
$("div#updates").append(html);//Append the html returned by the server .
}
});
}
return false;
});
});
现在我觉得这应该可以像这样扩展。
$(function() {
$.ajax({
type: "POST",
url: "/populate_sql.php",
beforeSend: function() {
$('a.load_more').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request
},
sucess: $('.load_more').live("click",function() {
var photoid = document.getElementById('photoid').value;
var lastid = document.getElementById('lastid').value;
if(lastid!='end'){
$.ajax({
type: "POST",
url: "/more_comments_ajax.php",
data: {
photoid : photoid,
lastid : lastid
},
beforeSend: function() {
$('a.load_more').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request
},
success: function(html){//html = the server response html code
$("#more").remove();//Remove the div with id=more
$("div#updates").append(html);//Append the html returned by the server .
}
});
}
return false;
});
});
});
我在哪里丢的?
您可以使用此函数。我以前用过,效果很好。在第一个回调接收到之后,它发送第二个请求。
(function($)
{
var ajaxQueue = $({});
$.ajaxQueue = function(ajaxOpts)
{
var oldComplete = ajaxOpts.complete;
ajaxQueue.queue(function(next)
{
ajaxOpts.complete = function()
{
if (oldComplete) oldComplete.apply(this, arguments);
next();
};
$.ajax(ajaxOpts);
});
};
})(jQuery);
像使用普通ajax一样使用它。示例:
$.ajaxQueue({ url: 'x.php', data:{x:x,y:y}, type: 'POST',
success: function(respond)
{
.....
}
});
因此,您可以检查是否有来自第一个ajax的回调,然后发送第二个请求。希望对你有所帮助。
谢谢你的回答。这并不是我所需要的,但它给了我一个想法,对我来说有效的解决方案是两个java脚本协同工作。如果有人需要类似的东西,我会把代码留在这里。
<script type="text/javascript">
jQuery(function($){
var pid = '<?php echo $ids['0']; ?>';
$.ajax({
type: "POST",
url: "/prepare_sql.php",
data: "pid="+ pid,
beforeSend: function() {
$('div#updates').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request
},
success: function(html) {
$("div#updates").replaceWith(html);
}
});
});
</script>
<script type="text/javascript">
$('.load_more').live("click",function() {
var photoid = document.getElementById('photoid').value;
var lastid = document.getElementById('lastid').value;
if(lastid!='end'){
$.ajax({
type: "POST",
url: "/more_comments_ajax.php",
data: {
photoid : photoid,
lastid : lastid
},
beforeSend: function() {
$('a.load_more').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request
},
success: function(html){//html = the server response html code
$("#more").remove();//Remove the div with id=more
$("div#updates").append(html);//Append the html returned by the server .
}
});
}
return false;
});
</script>
相关文章:
- Javascript-如何让脚本与Ajax请求的数据一起运行
- PHP脚本在Lion上通过Ajax运行时会回显自己的内容
- 使用ajax运行php脚本不会显示任何结果
- 通过ajax运行php页面时,请检查javascript中单击了哪个按钮
- 使用 AJAX 运行实时时钟
- 使用Javascript/Ajax运行php脚本
- php脚本通过ajax运行时显示进度条
- 如何使用ajax运行php文件
- Jquery+Ajax:运行一个不刷新页面的插件
- 希望JS函数使用ajax运行PHP,然后在页面上输出PHP
- 使用ajax运行python脚本
- 如何在ajax运行时禁用click功能,相同的click启动ajax运行,在ajax完成后,重新启用click功能
- 使用ajax运行php文件并在页面上输出php
- 如何设置ajax运行每5秒
- ajax运行时显示进度条或旋转器
- 通过链接通过Ajax运行PHP脚本
- 如何使用Ajax运行外部PHP文件并在同一页面中显示结果
- AJAX 运行正常,但不显示提取的数据
- 脚本在foreach内部不起作用,如何通过ajax运行它
- Ajax运行函数在我的类原型