Page 1 of 1

Incorrect encoding on JSONP API

Posted: Wed Feb 24, 2016 11:04 am UTC
by Pishmoffle
Today's comic (#1647) has revealed an encoding issue on the alt text of the JSONP-API, which I am using for an info-screen at our office. :)

(I cannot use the normal JSON API, since it doesn't set an Access-Control-Allow-Origin-header, and therefore cannot use it for cross-origin requests.)

Sadly I cannot link the APIs in question as that's considered spam by the forum software, but hopefully it's possible to deduce which APIs I'm talking about.

Re: Incorrect encoding on JSONP API

Posted: Wed Feb 24, 2016 11:21 am UTC
by Pishmoffle
Hmm. Actually looking at the Access-Control-Allow-Origin header of the API I'm using, it appears to be internal. (Oh dear.)

In any case, the bug is still there, and it'd be lovely to get a Access-Control-Allow-Origin header on the normal JSON API, so that it can be used from AJAX calls. :)

Re: Incorrect encoding on JSONP API

Posted: Wed Feb 24, 2016 11:35 am UTC
by phlip
To be specific, in the info.0.json for the strip, the word "forté" is encoded as "fort\u00c3\u00a9", which is definitely messed up.

[edit] And the transcript (which is, as separately noticed, in the json for 1649) is even worse:

Code: Select all

"transcript": "Typed letter: Attached please find my r\u00c3\u00a9s\u00c3\u00bcme[MANY STRANGE MARKINGS OVER AND BENEATH THIS LAST LETTER]\n\nI usually leave out diacritics when I type, so I make up for it by occasionally adding a whole bunch at once.\n\n{{Title text: Using diacritics correctly is not my fort\u00c3\u0083\u00c2\u00a9.}}"
Like... "r\u00c3\u00a9s\u00c3\u00bcme" is just more of the same, but "fort\u00c3\u0083\u00c2\u00a9" has somehow been double fucked. It's not even consistent within the same string.

And the JSONP goes one step further, encoding these Unicode characters again as direct UTF-8 (not \u escapes), and then serves it with no encoding tag in the content-type header, so the browser would need to guess it's UTF-8 just to not add yet another level of misencoding to the data. From my testing, it seems to depend on whether the HTML file that includes the JS file is UTF-8 encoded or not, which is madness, but that's the browser world for you... (you can get around this last hiccup by using <script charset="utf-8">, or by adding scriptCharset:'utf-8' to the $.ajax call if you're using jQuery).

Re: Incorrect encoding on JSONP API

Posted: Sat Feb 27, 2016 6:56 pm UTC
by Ajedi32
Seconding the request for Access-Control-Allow-Origin. I've encountered the same problem: /viewtopic.php?f=10&t=113315

I didn't know there was a JSONP API. Where is that documented?

Re: Incorrect encoding on JSONP API

Posted: Mon Feb 29, 2016 3:56 am UTC
by phlip

Re: Incorrect encoding on JSONP API

Posted: Fri Apr 14, 2017 4:52 pm UTC
by PM 2Ring
It's still there. In https://xkcd.com/1824/info.0.json we have

Code: Select all

alt": "Be careful—it's breeding season, and some of these can be *extremely* defensive of their nests.",


BTW, this bug was mentioned on Stack Overflow Meta.