简体   繁体   English

jQuery AJAX计时问题

[英]jQuery AJAX timing issue

In my backend I'm using jquery 1.4.1 and the newest UI 1.8rc1. 在后端,我正在使用jquery 1.4.1和最新的UI 1.8rc1。 I defined a couple of buttons that do things... one is create a certain type of page using serialize functions calling a php file and then reloading the entire page. 我定义了几个执行任务的按钮...一个是使用调用php文件的序列化函数创建某种类型的页面,然后重新加载整个页面。 locally this always works like a charm! 在本地,这始终像一种魅力! but as soon as i put it on my providers webserver, it only works in about 5% of times. 但是,一旦我将其放在提供商网络服务器上,它就只能在大约5%的时间内工作。 Heres the code: 这是代码:

    buttons: {
'Seite erstellen': function() {
$.post("webadmin/pages.create.serialize.php",$("#page-form").serialize());
$(this).dialog('close');
location.reload(true);
},
'Abbrechen': function() {
$(this).dialog('close');
}
},

Where it gets interesting is, when I put in an alert just before the location.reload part - it will always work. 有趣的是,当我在location.reload部分之前放置警报时,它将始终有效。 So there seems to be a timing issue that the serializing is executed but can't finish before the page reloads. 因此,似乎存在时序问题,即序列化已执行但在页面重新加载之前无法完成。 i know the meaning of using the serialzing is not to have to reload the page, but i build a navigation etc. so i need to reload. 我知道使用序列化的意思是不必重新加载页面,但是我建立了导航等。因此我需要重新加载。 (thinking about that now... i could really serialize everything... anyway) Is there a simple solution to this? (现在考虑一下……我真的可以序列化所有内容……无论如何)是否有一个简单的解决方案? is there something like a little timer i could build in to make it wait until the serialization is done? 有没有类似的小计时器,我可以内置它以使其等待序列化完成? is this a normal behaviour? 这是正常行为吗?

You need to take advantage of the callback in the $.post() method: 您需要利用$ .post()方法中的回调:

$.post(
       "webadmin/pages.create.serialize.php",
       $("#page-form").serialize(),
       function(data, textStatus, xhr) {  
           alert("I'm done loading now!");
       }
);

Not exactly sure what "this" refers to inside of the callback function so I'll leave the implementation as an exercise to the reader. 不能完全确定回调函数内部是什么“ this”,因此我将把实现留给读者练习。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM