Refactored the code to handle ajax post & requests