Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 531
Nicsoft Avatar asked Aug 24 '11 18:08

Nicsoft


People also ask

What is difference between GET and POST in jQuery?

GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data. POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.

Which jQuery function is used to send Ajax jQuery using POST method?

jQuery - AJAX get() and post() Methods. The jQuery get() and post() methods are used to request data from the server with an HTTP GET or POST request.


1 Answers

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!

like image 177
Diodeus - James MacFarlane Avatar answered Nov 04 '22 04:11

Diodeus - James MacFarlane