jQuery AJAX timing issue
In my backend I'm using jquery 1.4.1 and the newest 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. 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. 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. 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开发者_C百科 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(
"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. :-)
精彩评论