better query and error control for ajax request