我有一个dropwizard服务器和一个使用react和redux的通用应用程序。当我尝试使用通用应用程序中的axios进行后期调用时。我收到net :: ERR_CONNECTION_RESET错误。
我在dropwizard中设置了CORS:
FilterRegistration.Dynamic filter = environment.servlets().addFilter("CORS",CrossOriginFilter.class);
filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
filter.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,PUT,POST,DELETE,OPTIONS");
filter.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
filter.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
filter.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "*");
我有来自jetty的以下调试日志:
DEBUG [2016-03-07 05:42:04,189] org.eclipse.jetty.servlets.CrossOriginFilter: Cross-origin filter configuration: allowedOrigins = *, allowedMethods = GET,PUT,POST,DELETE,OPTIONS, allowedHeaders = *, preflightMaxAge = 1800, allowCredentials = true,exposedHeaders = ,chainPreflight = true
来自axios的请求是这样的
request
.post('http://localhost:8080/auth/register',{
username: 'foo',
password: 'bar'
})
.then((res) => {
console.log(res);
})
.catch((err) => console.log(err))
如果我从控制台使用简单的XmlHTTPRequest
,它也可以工作 var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
console.log("OK");
}
};
xhttp.open("POST", "http://localhost:8080/auth/register", true);
xhttp.send(JSON.stringify({
username: 'foo',
password: 'bar
}));