Why the request from the Javascript client to the Java server is not running?

Wrote a calculator using Javascript and I need to every time you click on the equals sign, two numbers and a sign went on the server and the server return me the result. Examples I made a server and it runs on my 3001 port. The post request I'm sending using XMLHttpReguest. Error is: Failed to load resource: net::ERR_FAILED

q7U7h.png
39R14.png
NOmNI.png
xr6pH.png

Server.java

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

class Request {
 private float firstNumber;
 private float secondNumber;
 private String action;

 public Request(float firstNumber, float secondNumber, String action) {
 this.firstNumber = firstNumber;
 this.secondNumber = secondNumber;
 this.action = action;
}

 public float getSecondNumber() {
 return secondNumber;
}

 public String getAction() {
 return action;
}

 public float getFirstNumber() {
 return firstNumber;
}
}

@RestController
@EnableAutoConfiguration
@RequestMapping(value = "/get-result", method = { RequestMethod.GET, RequestMethod.POST })
public class Example {
@PostMapping()
 public float xz(@RequestBody Request request) {

 float result = calc(request.getFirstNumber(), request.getSecondNumber(), request.getAction());

 return result;
}

 private float calc(float a, float b, String oper) {
 float result = 0;
 switch (oper) {
 case "plus": result = a + b; break;
 case "minus": result = a - b; break;
 case "multiple": result = a * b; break;
 case "devide": result = a / b; break;
}
 return result;
}

 public static void main(String[] args) {
 SpringApplication.run(Example.class, args);
}

}


Client javascript

function total(){
 let requestBody = {
 firstNumber: resultNumber,
 secondNumber: parseInt(result.innerHTML),
 sign: arrOfOperations[0]
};
 xhr.open("POST", 'http://localhost:3001/get-result');
 xhr.setRequestHeader("Content-Type", "application/json");
 xhr.onreadystatechange = function () {
 if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText);
 resultNumber = JSON.parse(xhr.responseText);
 result.innerHTML = resultNumber
}
};
 let data = JSON.stringify({requestBody});
xhr.send(data);
 temp = arrOfOperations[0];
}
April 7th 20 at 15:32
3 answers
April 7th 20 at 15:34
you have a lock on CORS—É, a cross-domain request
April 7th 20 at 15:36
April 7th 20 at 15:38

Find more questions by tags JavaJavaScriptSpring