In my web app, I submit some form fields with jQuery's $.getJSON()
method. I am having some problems with the encoding. The character-set of my app is charset=ISO-8859-1
, but I think these fields are submitted with UTF-8
.
How I can set encoding used in $.getJSON
calls?
You need to analyze the JSON calls using Wireshark, so you will see if you include the charset in the formation of the JSON page or not, for example:
Why is that? because we can not put on the page of JSON a goal like this:
In my case I use the manufacturer Connect Me 9210 Digi:
It worked for me without having to convert the data passed by JSON for UTF-8 and then redo the conversion on the page ...
Use
encodeURI()
in client JS and useURLDecoder.decode()
in server Java side works.Example:
Javascript:
Java:
java.net.URLDecoder.decode(params.user, "UTF-8");
If you want to use
$.getJSON()
you can add the following before the call :You can use the charset you want instead of
utf-8
.The options are explained here.
contentType :
When sending data to the server, use thiscontent-type
. Default isapplication/x-www-form-urlencoded
, which is fine for most cases.scriptCharset :
Only for requests withjsonp
orscript
dataType and GET type. Forces the request to be interpreted as a certain charset. Only needed for charset differences between the remote and local content.You may need one or both ...
I think that you'll probably have to use
$.ajax()
if you want to change the encoding, see thecontentType
param below (thesuccess
anderror
callbacks assume you have<div id="success"></div>
and<div id="error"></div>
in the html):I actually just had to do this about an hour ago, what a coincidence!
f you want to use $.getJSON() you can add the following before the call :
Use this function to regain the utf-8 characters
Example: var new_Str=decode_utf8(str);