update page data using ajax
i have开发者_高级运维 a treeview that shows my server directory structure. the user enters a folder name and i create that folder in my root directory at the server. my code is as follows
$.post("makeDir.php", {data:path}); //makeDir.php creates the folder using mkdir()
location.reload(true);
im using location.reload(true)
to show user the updated tree. now the problem is that sumtimes, the 'post' request is not completed yet and it reloads the page soon enuf that the newly created folder is not displayed in the treeview. secondly im also not sure if using location.reload(true) is the proper way to do it!! in this situation shud i use $.ajax() function?
Use the success
param of $.post
$.post("makeDir.php", {data:path}, function() {
location.reload(true);
);
This will make sure that the page reloads only after the request is completed.
$.post("makeDir.php", {data:path},function(){
location.reload(true);
});
If you plan to reload the page — don't use Ajax. Just submit a form.
If you are going to use Ajax — don't reload the page. Have the server return some data, then process that (updating the DOM) with the callback (the third argument to $.post
).
精彩评论