WIP: Improve behavior of the AJAX.

#5817 

Don't merge this yet. This is WIP.