I have a deployed spring server on my EC2 instance which is throwing this error. For context, from my index.html, I have a form with a submit button, which sends a post call. This post call is in my resource. The POST call works perfectly fine on my localhost, but when I deploy it on EC2, it throws an error in the title. This is also all built on spring boot.
HTML
<script type="text/javascript" src="js/Email.js"></script>
(other divs with headers and titles in between)
<form onsubmit="sendEmail(this)">
(input tags over here and a button)
</form>
Email.js
function sendEmail(form) {
var bodyVal = "{\"email\" : \"" + "[email protected]" + "\",\"message\" : \"" + "hello this is message" + "\"}"
const req = new XMLHttpRequest();
var myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/json');
fetch("http://(domain name)/sendmail", {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
headers : myHeaders,
body: bodyVal
}).then(function(response) {
});
return false;
}
Resource which targets the /sendmail endpoint
@CrossOrigin
@PostMapping(path = "/sendmail", consumes = "application/json", produces = "application/json")
public void sendMail(@RequestBody Map<String, Object> payload) throws Exception {
String message = payload.get("message").toString();
String fromEmail = payload.get("email").toString();
String body = "";
Unirest.post("https://api.sendgrid.com/v3/mail/send")
.header("authorization", SEND_GRID_AUTH)
.header("content-type", "application/json")
.body(body)
.asString();
}
I've seen some solutions of putting some extra tags in the html to solve some security issues, but I'm not sure what's the proper solution for this. If someone could help me out it would be great.
@Controllerinstead of@RestController