1

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!");
}
2
  • the problem is that the script doesn't work even when I put it inside the script tag, not through src Commented Oct 13, 2021 at 13:54
  • 1
    I can go to the send.js file with ctrl+left click, so I guess pathing to the file is ok Commented Oct 13, 2021 at 13:56

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I changed it and it still doesn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.