Why the capacitors works on ac only?

Generally capacitor gives infinite resistance to dc components (i.e., block the dc components). it allows the ac components to pass through.