I am having trouble passing a JSON object to my controller using AJAX. The issue I am facing is a CORS error that reads as follows:
Access to XMLHttpRequest at ........ from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Below is the Javascript and AJAX call code snippet:
database.on('child_added', function (snapshot) {
var data = {};
data["CaptainName"] = snapshot.val().CaptainName;
data["CookName"] = snapshot.val().CookName;
data["LogicalConditions"] = snapshot.val().LogicalConditions;
$.ajax({
headers: { 'Access-Control-Allow-Origin': '*' },
type: "GET",
contentType: "application/json",
url:"my-localhost/application/print",
data: JSON.stringify(data),
dataType: 'json',
cache: false,
success: function(){
console.log("Successfully sent payload")
},
error: function(e){
console.log("Error:" , e)
}
});
Now, here is the controller logic:
@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
@RequestMapping("/application")
public class AppController
{
@PostMapping("/print")
public void print(@RequestBody String st)
{
System.out.println(st);
}
}
Any suggestions on how to resolve this CORS error?