24 VAC Detection Using Optocoupler
You must not apply a reverse voltage of more than 6 V to the LED. (In practice, the actual breakdown voltage will be higher than that, but 24 VAC certainly is high enough to be too risky.)

You could


use a bridge rectifier to route both half waves of the input through the LED, or
connect an antiparallel diode to the optocoupler's LED pins to limit the reverse voltage to the forward voltage of the diode, or
put a properly rated diode in series with the optocoupler's LED to prevent the LED from being destroyed by the breakdown current.