I am using simpleCORSFilter.java for handling CORS request, which is working fine for other request but somehow, it is not working for AJAX POST request.
simpleCORSFilter.java
@Component
public class SimpleCORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin,X-Requested-With,X-Requested-by, Content-Type, Accept,Session");
response.setHeader("Access-Control-Expose-Headers", "Location");
chain.doFilter(req, res);
}
Ajax request
$.ajax({
url: url,
dataType: 'json',
type: 'POST',
contentType: 'application/json; charset=UTF-8',
crossDomain: true,
data: JSON.stringify(data),
success: function (data) {
console.log(' data : '+data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('jqXHR:');
console.log(jqXHR);
console.log('textStatus:');
console.log(textStatus);
console.log('errorThrown:');
console.log(errorThrown);
}
});
After sending the preflight response it just stops working(doesn't shutdown server, but it is like just hanging there without any response), and if I try to request anything else then, it will break the connection and respond following
Caused by: java.io.IOException: An established connection was aborted by the software in your host machine
Server Log :
----------------------------
ID: 1
Address: http://localhost:9092/services/searchCMS/
Encoding: UTF-8
Http-Method: OPTIONS
Content-Type:
Headers: {Accept=[*/*], accept-encoding=[gzip, deflate, sdch], accept-language=[en-US,en;q=0.8], access-control-request-headers=[content-type], access-control-request-method=[POST], connection=[keep-alive], Content-Type=[null], dnt=[1], host=[localhost:9092], origin=[http://localhost:3000], referer=[http://localhost:3000/report], user-agent=[Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36]}
--------------------------------------
2016-08-12 09:50:48.912 INFO 6408 --- [nio-9092-exec-1] o.a.c.interceptor.LoggingOutInterceptor : Outbound Message
---------------------------
ID: 1
Response-Code: 200
Content-Type: text/xml
Headers: {Allow=[POST, GET, PUT, OPTIONS, HEAD], Date=[Fri, 12 Aug 2016 07:50:48 GMT], Content-Length=[0]}
--------------------------------------
I don't understand where exactly is the problem , Ajax part or Web Service part ? I have read so many blogs and stackoverflow answers, but I was not able to solve this problem.It just simply throws error after after getting preflight response from the web service.I don't know why ? may be I am missing something, which you experts can point out.