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

169
Visualizações
javascript XMLHttpRequest needs controller?

Spring Boot 2.6.2
Java 11
Thymeleaf

directory

My goal is to load navbar.html through javascript when categoryTest.html is opened.
First of all, I add this code to categoryTest.html

        <nav class="navbar" includeHTML="navbar.html">
        </nav>

And there is the function I wrote

    function includeHTML() {
        var z, i, elmnt, file, xhttp;

        z = document.getElementsByTagName("*");

        for (i = 0; i < z.length; i++) {
            elmnt = z[i];

            file = elmnt.getAttribute("includeHTML");

            if (file) {
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function() {
                    if (this.readyState == 4) {
                        if (this.status == 200) {elmnt.innerHTML = this.responseText;}
                        if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
                    }
                }
                xhttp.open("GET", file, true);
                xhttp.send();
                return;
            }
        }
    }

Whenever I start the server and see the result, xhttp.send() is the problem.

But once I add this method to my controller, it works well.

    @GetMapping("/navbar.html")
    public ModelAndView navbar(){
        ModelAndView modelAndView = new ModelAndView("navbar");
        return modelAndView;
    }

Idk why this method is required for this process.

about 4 years ago · Juan Pablo Isaza
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