Jump to content

HttpRequest e caracteres especiais


PRSousa
 Share

Recommended Posts

Isso acontece por causa do encoding do response ou do encoding que está definido no HTML.

Tens de definir o header com o enconding correcto.

Qual é a linguagem que utilizas do lado do servidor (PHP,ASP,RUBY,PHYTON)?

Tens o encoding no HTML definido para iso-8859-1.

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

Na resposta tens de enviar o header com o encoding iso-8859-1.

Exemplo em PHP:

<?php
header('Content-Type: text/html; charset=iso-8859-1');
echo 'Olá mundo do PHP';
?>
Link to comment
Share on other sites

Obrigado desde já pelas respostas, e passo então a enviar os pedaços de código:

A minha página contém:

...
  <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  <script language="JavaScript" type="text/javascript">
  function GetHTML()
    {
    var url = "http://www.exemplo.pt/default.html";
    if (window.XMLHttpRequest)
      {
      xhttp = new XMLHttpRequest();
      }
    else
      {
      xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
    xhttp.open( "GET", url , false );
    xhttp.setRequestHeader( "Content-Type", "text/html; charset=windows-1252" );
    xhttp.send(null);
    document.getElementById("OtherPageHTML").innerHTML = xhttp.responseText;
    xhttp = null;
    }
...
   <iframe id="iframe" src="http://www.exemplo.pt/default.html"></iframe>
   <input type="submit" onclick="GetHTML()" value="Get Page" />
   <div id="OtherPageHTML"></div>
...

Na <iFrame> aparece tudo direitinho mas na <div> aparece tudo estropiado.

Analizando a página pretendida (http://www.exemplo.pt/default.html) verifica-se que tem:

...
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
...

Podem ajudar-me p. f.

Obrigado

Link to comment
Share on other sites

Tenta antes assim:

<meta http-equiv="Content-Type" content="text/html; charset=charset=iso-8859-1">
  <script language="JavaScript" type="text/javascript">
  function GetHTML()
    {
    var url = "http://www.exemplo.pt/default.html";
    if (window.XMLHttpRequest)
      {
      xhttp = new XMLHttpRequest();
      }
    else
      {
      xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
    xhttp.open( "GET", url , false );
    xhttp.setRequestHeader( "Content-Type", "text/html; charset=iso-8859-1" );
    xhttp.send(null);
    document.getElementById("OtherPageHTML").innerHTML = xhttp.responseText;
    xhttp = null;
    }
...
   <iframe id="iframe" src="http://www.exemplo.pt/default.html"></iframe>
   <input type="submit" onclick="GetHTML()" value="Get Page" />
   <div id="OtherPageHTML"></div>

PS: Não respondo a perguntas por mensagem que podem ser respondidas no fórum.

Link to comment
Share on other sites

Como já disse, a página não é minha mas está num domínio a que tenho acesso. Não vale a pena fornecer o URL pois não consegues ver.

Tenho de usar a página tal qual ela está, ou seja, não posso fazer nada ao nível do servidor.

Vês alguma alternativa?

Há alguma forma de usar a <iframe>, uma vez que neste container o HTML está direitinho?

Obrigado.

Link to comment
Share on other sites

Tens duas formas de resolver o problema do enconding.

Primeira:

- Identificar o encoding da página pedida (podes fazer isso utilizando o firebug na aba NET e depois abres o link da página na aba HEADERS).

- Colocar o teu HTML com o mesmo encoding da página pedida.

Segunda:

- Buscar o conteúdo em PHP (por exemplo: file_get_contents) e converter o conteúdo para o encoding correcto.

Link to comment
Share on other sites

Pois isso já eu tentei, por isso estou perdido.

Primeira:

O enconding da págima pretendida é:

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

que é exactamente o que eu coloquei na minha página:

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

e que coloquei também no XHR:

xhttp.setRequestHeader( "Content-Type", "text/html; charset=windows-1252" );

Não funciona :-(

Segunda:

PHP não uso. Há alguma alternativa em JavaScript que permite fazer esta extracção convertendo para o encoding correcto?

Obrigado

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.