I'm trying to send HTTP POST to the my server, I do it in this way:
var headers = new Headers({ 'Access-Control-Request-Headers': "Content-Type", 'Content-Type': 'application/json'});
let options = new RequestOptions({ headers: headers });
console.log(JSON.stringify(library) + " " + SERVER + "/library");
this.http.post(SERVER + "/library", JSON.stringify(library), options)
.subscribe(data => {console.log(data.json())});
but I'm getting Refused to set unsafe header "access-control-request-headers"
error. If I'm sending POST without 'Access-Control-Request-Headers': "Content-Type"
I'm getting Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response
. Does anyone know how to solve this problem? Thank in advance.
I've solved this problem by adding next code on the server side:
public class CORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, content-type");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
}
Thanks, Günter Zöchbauer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With