Jquery / Ajax cookie pt2

继上一个问题( 上一个问题 )之后,如果检测到cookie,我似乎无法“触发”ajax调用。 cookie肯定是设置好的,并且会显示警报,但我不能在我的生活中让它再次触发ajax调用。 如果检测到cookie,我只需要在加载页面时“触发”ajax,而不是使用“更多”按钮。

希望这是有道理的。 任何帮助非常感谢。 S.

$(document).ready(function(){ $(function() { //More Button $('.more').live("click",function() { $.cookie('viewing_expanded_content',true, { expires : new Date(new Date().valueOf() + 60 * 60 * 1000) }); var ID = $(this).attr("id"); if(ID) { $("#more"+ID).html('More results...'); $.ajax({ type: "POST", url: "more_press.php", data: "lastmsg="+ ID, cache: false, success: function(html){ $("div#updates").append(html); $("#more"+ID).remove(); } }); } else { $(".morebox").html('

No more results...

'); //$.cookie('viewing_expanded_content', null); } return false; }); }); var viewing_expanded_content = $.cookie('viewing_expanded_content'); if ( viewing_expanded_content == 'true' ) { alert("Active cookies!"); //my proposed call that doesnt work $.ajax({ type: "POST", url: "more_press.php", data: "lastmsg="+ ID, cache: false, success: function(html){ $("div#updates").append(html); $("#more"+ID).remove(); } }); } })

编辑: 这里的工作解决方案……

要设置错误回调function,您可以这样做

 //my proposed call that doesnt work $.ajax({ type: "POST", url: "more_press.php", data: "lastmsg="+ ID, cache: false, success: function(html){ $("div#updates").append(html); $("#more"+ID).remove(); }, error: function(){ // write your code for error handling } });