Generally the spelling with x is the Old Spanish (castellano antiguo) spelling. In the Old Spanish you'd have for example Don Quixote. This changed with establishment of The Real Academia Española, which published in 1754 published new edition of "Ortografia de la lengua castellana", which pretty much defined modern spelling. One of the changes was that the /x/ sound would be spelled with j instead of x, while x would only represent /ks/ sound.
However, México, being a proper name is exception from the rule. In fact México is the only spelling appearing in the official dictionary of Real Academia Española. Spelling Méjico is still sometimes used in Spain, but it's never used elsewhere in Spanish speaking countries.
The names of places stayed with the old spelling for historical reason, this is true for Mexico, Oaxaca and Texas, however names like Xavier had to use the new spelling Javier.
They also changed names that start with big I (Iota) now Jota (J) like Iesus changed to Jesus