Problem with accents and special characters, called Ajax

Asked

Viewed 243 times

0

I have a function that makes an ajax call, and sends the data to be saved in the database, the problem is that whenever you save a word that has some special character or accent, it does not encode in the correct way,

I’ve tried using the :

encodeURIComponent(string)

pórem did not show the result experienced, my function is as follows

function relatorio() {

    var tipo = document.getElementById('tipo').value;
    $.ajax({
        method: "POST",
        url: "relatorio.asp",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            tipo: encodeURIComponent(tipo) , exs: "relatorio_tipo"
        }
    });
}

if I send a type = "function" saved on the seat as follows = "fun&231&227o"

I don’t know if I’m doing it right or if I’m missing something would like to know a solution so that my variable is saved in the correct way.

code that is in the report.

if ex = "relatorio_tipo" then

Response.Charset="ISO-8859-1"
tipo= trata_sql(request("tipo"))

 set rs = Server.CreateObject("ADODB.Recordset")
 rs.LockType = 2
 rs.Open "relatorios", conexao
 rs.addnew
 rs("tipo") = tipo
 rs("data") = now()
 rs.Update

rs.close
set rs = nothing
conexao.close
set conexao = nothing

end if
  • Which database is it? It may be the encoding of the databank that is not accepting special characters Or try to pass this charset to iso 'Content-type: text/html; charset=iso-8859-1'

  • the database is mysql, I realized that in the date:{ type: encodeURIComponent(type)} the variable does not encode, when I give a print to see what it brings does not return in the correct way, I tried to use Content-type: text/html; charset=iso-8859-1 and the problem is still continuing and the type is equal = fun%C3%A7%C3%A3o

  • It may be that the problem is in the same bank, you can take the encoded string and do a direct Insert in the table to see if it is saved correctly?

2 answers

2

One possibility is to remove the encodeURIComponent, I believe that you will not need to use the encodeURIComponent in your scenario, because AJAX is using the method POST, its method was GET it would be interesting to use encodeURIComponent, however you will need to use a function for Decode in Asp.

function relatorio() {

    var tipo = document.getElementById('tipo').value;
    $.ajax({
        method: "POST",
        url: "relatorio.asp",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            tipo: tipo
        }
    });
}

And in your file relatorio.asp change the included Charset at the beginning of the file the command Response.Charset="UTF-8"

  • tried this way too and unfortunately did not solve the problem, in the bank saves exactly the way the variable is loading,, for example if I give an Alert() in the variable type before the date: { type: encodeURIComponent(type) } it brings the variable the right way "function" and within the date the variable type already loads differently "fun&231&227o" , I tried several possibilities and still could not understand the reason

  • @Jess u removed the encodeURIComponent ??

  • I tried with him and without also

  • @Jess has how to post your report code.

  • and I realized that with the encodeURIComponent it brings the variable in this way "fun&231&227o" and without it stays the same way, it’s like it’s not making a difference it

  • I updated the question and put the code I have in the report.

  • exchange Response.Charset="ISO-8859-1" for Response.Charset="UTF-8"

  • I made the trade and the problem continues

  • in the.Asp report, I’m having a Report.write on the variable type before creating and always comes the same way "fun&231&227o"

Show 4 more comments

0

Well I finally managed to solve this problem, for those who are having a similar problem what I did was the following:

function relatorio() {

    var tipo = document.getElementById('tipo').value;

first I made an encodeURIComponent() of variable type

t = encodeURIComponent(tipo); 

$.ajax({
    method: "POST",
    url: "relatorio.asp",
    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
    data: {
     //aqui eu fiz um decodeURIComponent(t) 
       tipo: decodeURIComponent(t) , exs: "relatorio_tipo"
    }
});

}

this way the problem solves.

Browser other questions tagged

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