Access-Control-Allow-Credentials' header in the response is "" which must be 'true'

Viewed 5121

I'm trying to send a comment to a REST api. That rest API has the CORS already set for my app address..

BackEnd

@RestController
@CrossOrigin(origins = "http://localhost:8000", allowedHeaders = "*", methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.DELETE,RequestMethod.PUT})
@RequestMapping("/api")
public class CommentController {

    @Autowired
    private CommentRepository commentRepository;

    // Get All Comments From a certain workitemId
    @GetMapping("/comments/{workitemId}")
    public List<Comment> getTicketHistory(@PathVariable Long workitemId) {
        return commentRepository.getCommentsByWorkitemId(workitemId);
    }

    // Create a comment related with a given Workitem
    @PostMapping("/comment")
    public boolean createComment(@RequestBody Comment comment) {
        commentRepository.save(comment);
        return true;
    }
}

But I get

Failed to load http://localhost:8999/api/comment: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. Origin 'http://localhost:8000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

My Code:

$http.post(baseUrl + "/comment", vm.comment).then(
      function(response) {
        // success callback
        console.log("Comment Submitted!");
      },
      function(response) {
        // failure call back
         console.log("Error while submitting the comment");
      });
2 Answers

Try adding allowCredentials = "true" in @CrossOrigin annotation as,

@CrossOrigin(
    allowCredentials = "true",
    origins = "http://localhost:8000", 
    allowedHeaders = "*", 
    methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.DELETE,RequestMethod.PUT}
)

This might work.

You forgot to add:

allowCredentials=true

It shoul be:

@CrossOrigin(origins = "http://localhost:8000", allowCredentials = "true", allowedHeaders = "*", methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.DELETE,RequestMethod.PUT})
Related