Encoding UTF-8 being interpreted as Windows-1252

542 views
Skip to first unread message

james foss

unread,
Apr 4, 2017, 4:51:49 AM4/4/17
to Mojolicious
Hi,

I'm having problems decoding the britsh pound sign (£) in Mojolicious.

I'm making an ajax PUT request and my js is something like this:
...
method: "PUT",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: function ( d ) {
     return encodeURIComponent(JSON.stringify( d.data ));
}

I am using encodeURIComponent to deal with &. The £ gets encoded as %C2%A3.

In Mojolicious I am using:

my $json = $self->req->json;
$json->{data} = url_unescape $json->{data};
$json->{data} = decode_json $json->{data};

Dumping the data shows that the pound sign is being decoded as £.

I've tried using:

my $json = url_unescape $json->{data};
$json = decode_json $t->encode('UTF-8');

Still no luck.  Can someone tell me how I should decode/encode this correctly please?

Tekki

unread,
Apr 4, 2017, 10:10:02 AM4/4/17
to Mojolicious
There should be no reason to encode a JSON string.

james foss

unread,
Apr 5, 2017, 4:31:50 AM4/5/17
to Mojolicious
The json data conatins &s which causes problems as identified here:

http://stackoverflow.com/questions/11294107/how-can-i-send-the-ampersand-character-via-ajax

Tekki

unread,
Apr 5, 2017, 9:21:32 AM4/5/17
to Mojolicious
This question on Stackoverflow is about PHP and a request with content-type application/x-www-form-urlencoded, not application/json. Maybe your problem is caused by the JavaScript library you use. I use Mojolicious together with Angular, run many of the UI tests with names that contain '&' and never had to do any encoding or decoding.
Reply all
Reply to author
Forward
0 new messages