Nicsoft
Nicsoft

Reputation: 3712

Posting and returning data using jquery corrupts å ä ö

This is a question with a lot of information on the net. Still, I haven't solved it.

I am having a dynamically loaded form on a web page. I submit it using jquery $.post. My page is for Swedes so it needs to work with åäö.

When posting data to server, it will then load the data back into a new table. The returned å ä ö is corrupted. Example åååäääööö = åååäääööö

The server side is php. I encode all pages that return content to the browser like this:

<?php header('Content-type: text/html; charset=ISO-8859-1'); ?>

I have tried utf8_encode and utf8_decode on the server side. No difference actually.

And i tried this: encodeURIComponent($(this).serialize()) where $(this) is the form to be submitted. Of course, this doesn't work and I don't know how to encode an object like $(this).

I thought that this is something that a lot of people does, but it doesn't seem to be a standard solution.

Hence, what I need help with is how can I post a form using javascript/jquery/etc. and handle it on the server side, returning it and å ä ö looks as they should. It's totally fine if the server receives utf8. Should the principle be to only use UTF8 and then the problem is solved? Seems like there should be another solution.

Upvotes: 6

Views: 2024

Answers (1)

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114347

AJAX uses UTF-8 encoding. If your data is not in UTF-8 format (such as Windows 1251 or ISO-8859-1) you're going to have trouble.

Specifying a different encoding scheme in the header doesn't convert your data to UTF-8. It just tells the client what to expect.

My Motto: UTF-8 end-to-end or die!

Upvotes: 4

Related Questions