A helicopter carrying him was diverted at the last minute after British intelligence officers uncovered plans to target a visiting VIP.
It also became clear the Taliban hoped to do it by downing a chopper in a rocket attack.
Cameron was about to visit the isolated Shahzad patrol base in Afghanistan's Helmand province.
But as soon as Taskforce Helmand commander Brigadier Richard Felton learned of the plot, he ordered the stopover to be cancelled. Cameron was flown instead to the main British operating base at Lashkar Gah.
He shrugged off the scare as he enjoyed burgers with troops at a barbecue there.
Government aides said the prime minster was "very disappointed" to have missed out on the visit to Shahzad -- home to around 80 troops from the 1st Battalion Duke of Lancaster's Regiment.
One aide added: "It was decided it was too risky."