The Oxford English Dictionary says that it apparently derives from the initials of orl korrect, a humorous alternative of all correct.
It gained popularity in the 1840 American Presidential election, referring to Old Kinderhook, a nickname of Martin Van Buren which was derived from his birthplace.
So by the middle of the 19th century OK was being used as an adjective, interjection and noun.
By the late 19th century OK was in use as an adverb and verb. It was also in this period it began to be spelled as okay.
Okey-doke, which is used as an adjective and interjection, didn't appear until the middle of the 20th century.
According to the OED its use in all situations has always been colloquial, or somewhat informal.