CORS 요청을 처리하기 위해 simpleCORSFilter.java를 사용하고 있는데 다른 요청에는 잘 작동하지만 어떻게 든 AJAX POST 요청에는 작동하지 않습니다.
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 요청
$.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);
}
});
프리 플라이트 응답을 보낸 후에는 작동이 중지됩니다 (서버를 종료하지 않지만 응답없이 거기에 매달려있는 것과 같습니다). 다른 요청을 시도하면 연결이 끊어지고 다음과 같이 응답합니다.
Caused by: java.io.IOException: An established connection was aborted by the software in your host machine
서버 로그 :
----------------------------
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]}
--------------------------------------
Ajax 부분 또는 웹 서비스 부분이 정확히 어디인지 이해가 안 되나요? 많은 블로그와 stackoverflow 답변을 읽었지만이 문제를 해결할 수 없었습니다. 웹 서비스에서 프리 플라이트 응답을받은 후 오류가 발생합니다. 이유를 모르겠습니다. 전문가가 지적 할 수있는 무언가가 누락되었을 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다