You have dirty contacts or a plug which doesn’t seat properly. Try wiping both sides with a dry q-tip (this is Apple’s recommendation) or use one soaked in isopropyl alcohol.
Sometimes little bits of magnetic debris can get stuck in the computer side, e.g. iron filings, staples, etc. You can try to get these out with Blu-Tack or similar if the q-tip doesn’t cut it.