Refactor search ajax code