Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!