Special Accents and Characters in Ajax jQuery

4

I have a query that is done with Ajax request via jQuery. In the fields when I type a character as "ç" and send the request to the server, it is arriving with the character in another format. Ex: I type ç in the name field and it arrives in Action this way: ç. I'm using Struts 1.

Ajax calling code:

var request = $.ajax({
            cache: true,
            url: "/listagem",
            data:{  action: "ajax", 
                    page_num: page_num,
                    categoria: options.categoria,
                    estado: options.estado,
                    cidade: options.cidade,
                    especialidade: options.especialidade,
                    nome: options.nome_prestador,
                    bairro: options.bairro_prestador,
                    area_atuacao: options.area_atuacao,
              }
            });
        request.done(function(data){
            montarHtmlLista(data);
        });

I tried to put the following parameters and it did not work:

contentType: "application/json; charset=ISO-8859-1",
dataType: "json",

This one too:

 beforeSend: function( xhr ) {
   xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
 }

My JSP already has the following lines:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="utf-8" isELIgnored="false"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    
asked by anonymous 09.05.2014 / 16:17

2 answers

2

When making Ajax requests that may have encoding problems (because they contain accents, tils, cedillas, or non-Latin characters (ie: Japanese, Arabic characters, etc.)), use the encodeURIComponent . This escapes the characters to something the server should be able to understand.

I.e.:

encodeURIComponent("ç"); // resulta em "%C3%A7"
encodeURIComponent("açaí"); // resulta em "a%C3%A7a%C3%AD"

For each string you are using that may contain special characters, do not use the string directly, but rather the return of encodeURIComponent on that string.

    
09.05.2014 / 16:22
1

You need to change the page encoding to ISO-8859-1.

Every encoding of your call application should generally have the same charset.

I recommend that you use utf-8 as an unicode international standard.

pageEncoding="utf-8"

or

pageEncoding="ISO-8859-1"
    
09.05.2014 / 16:21