简体   繁体   中英

To reload or not reload - Is it better to send data to the server via ajax and then reload the page to see the result or update the DOM using js?

I have a fairly complex Django (3.1) site. On a couple of pages, I have some html/javascript code for the user to add/edit/delete comments. The script packages the comment and other bits and sends it to the server in an ajax POST request so the comment can be added/updated/deleted to/from the database. The script then gets an acknowledgement from the server, and, currently, it reloads the page so the new/edited/deleted comment can be seen at the bottom of the page. It all works fine in Chrome and Firefox.

My questions is:

Is it better practice to have the script add/edit/delete the comment html directly in the DOM based on the ajax response, rather than just reloading the page with the updated data? Why or why not?

Thanks!

Mark

If you're only changing a string of text or some such, it makes no sense to require the user to go through the entire process of reloading the page with every request. Each page reload fires off numerous requests, even if only to the browser cache, which nonetheless takes time to load and redraw. This has a negative impact on the perceived performance of the application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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