Accents and Special Characters in Ajax jQuery

Asked

Viewed 8,876 times

5

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

Code called Ajax:

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 didn’t work:

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

That other 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">
  • Your application is made with JAVA?

  • Yes it’s done, because?

  • It would be nice to pass this information as a tag ... just a recommendation in your question

  • Vc says create a specific tag to solve the problem?

  • you put in jquery ajax tag I believe putting java would also be more complete your question, but, I leave it you decide. !!!

  • 1

    Got it now. I thought you were talking about the Taglib used in the JEE

Show 1 more comment

2 answers

2


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

I.and.:

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 about that string.

  • 1

    I got it the way you said it, and in my action I used a Urldecoder.Decode(). Thank you very much.

1

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

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

I recommend using utf-8 as it is an international Unicode standard.

pageEncoding="utf-8"

or

pageEncoding="ISO-8859-1"
  • 1

    I changed, but the problem continued. Thank you for the answer =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.