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

96
Visualizações
Spring Boot application won't run JavaScript code

I am building a spring web application that allows sending code snippets to a database. I have an HTML endpoint where you can enter some text and click a button so that the text is sent to a database. Here's the html code:

enter image description here

The send.js script is responsible for taking the input text and casting it to json. Here's the send.js code:

    function send() {
    let object = {
        "code": document.getElementById("code_snippet").value
    };

    let json = JSON.stringify(object);

    let xhr = new XMLHttpRequest();
    xhr.open("POST", '/api/code/new', false)
    xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
    xhr.send(json);

    if (xhr.status == 200) {
        alert("Success!");
    }
}

For some reason when I press the button nothing happens. I tried debugging the app and it seems like it doesn't execute the js code. I had some issues with even accessing the send.js in html at first, intellij just couldn't see it for some reason. This is my resources structure:

enter image description here

What am I doing wrong here?

Edit: I tried adding this instead of just checking if status is equal to 200 and I dont get either of the alerts

if (xhr.status != 200) {
    alert("Something went wrong!");
} else if (xhr.status == 200){
    alert("Success!");
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In the html file you have id="code-snippet", and in the js you have "code_snippet", try fixing that

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