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:
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:
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!");
}
在你有 id="code-snippet" 的 html 文件中,在你有“code_snippet”的 js 中,尝试修复它
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.