Rafactor and header

Refactoring method for performance and added X-Requested-With header to
indicate AJAX call