North Korea test-fired a ballistic missile from a location north of the capital Pyongyang, but it exploded shortly after liftoff, the US and South Korean militaries say.