In the 1840s telegraph communications via Morse code became a reality on th east coast of the United States. Telegraphers strove to use shortcuts in messages which would clarify the messages and complete them more quickly. To this day a letter k _._ or "dah dit dah" in morse code is transmitted at the end of each completed message, which is a question asking "have you got that ok?". The receiver then transmits an r ._. or "dit dah dit" in response which means, message received ok. The telegraphers paraphrased the k and r to "ok" and "roger" which both remain in the common parlance today. Although other more esoteric origins of "ok" exist, they were not in the common parlance at the time that telegraphers, train conductors and engineers popularized their use in the 1840s. The term spread to England and Continental Europe (even non-english speaking countries)as telegraphy spread to them.
The principal of establishing a "handshake" in communication in this way (utilizing complimentary status bits regarding segments of messages)is used to this day in advanced digital communication devices.
Please go to the store and buy a gallon of milk, OK?
OK, I will do that.