Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
Escaped characters appearing when appending to HTML element

I am fetching HTML code via an AJAX call, and then appending the code it to an existing HTML element. Problem is, the appended code contains escaped characters, causing the HTML to break, and resulting in haphazard elements looking like this: Contact Us ▸<\/a><\/li>\n: Contact Us ▸</a></li>\n

The HTML element is: <li class="listitems"><a href="https:\/\/website.com\/contact" class="listlinks">Contact Us ▸&lt;\/a&gt;&lt;\/li&gt;\n</a></li>

For reference, the ajax response received is:

"<li class='listitems'><a href='https:\/\/website.com\/contact' class='listlinks'>Contact Us &rtrif;<\/a><\/li>\n"

And the JavaScript code used to add this to the existing ul element is:

let r = httpRequest.responseText; // The AJAX response
document.getElementById('mylist').insertAdjacentHTML("beforeend",r);

I've also tried using the following:

let r = httpRequest.responseText; // The AJAX response
document.getElementById('mylist').interHTML += r;

but am observing the same results.

However, when I try either JavaScript code in the browser's console: I get the expected results: Contact Us ▸Contact Us ▸

And the HTML element is: <li class="listitems"><a href="https://website.com/contact" class="listlinks">Contact Us ▸</a></li>

I am unable to understand why the code is not being unescaped and evaluated as HTML normally, but it works when I do it manually using the browser's console window.

Can anyone tell me what I'm missing here and how I can fix this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The response is JSON, you need to parse it.

let responseText = `"<li class='listitems'><a href='https:\\/\\/website.com\\/contact' class='listlinks'>Contact Us &rtrif;<\\/a><\\/li>\\n"`;
let r = JSON.parse(responseText);
document.getElementById('mylist').insertAdjacentHTML("beforeend",r);
<ul id="mylist">
</ul>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda