How to run ajax in WordPress Post?

I believe one of the comments should fix your issue given how you mentioned that the code works outside of WordPress, which does in fact appear to be a problem with your references to $ instead of jQuery.

Also, i have written a post on this exact topic with step by step instruction, you can see it here:
Ajax Calls in WordPress