我想启用对我的wildfly服务器的跨域访问。
实际问题如下:
XMLHttpRequest cannot load http://192.168.149.127/myService. Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8100' is therefore not allowed access.
The response had HTTP status code 405.
所以我在standalone.xml
文件中添加了以下子系统:
<subsystem xmlns="urn:jboss:domain:undertow:3.0">
<buffer-cache name="default" />
<server name="default-server">
<http-listener name="default" socket-binding="http"
redirect-socket="https" />
<host name="default-host" alias="localhost">
<location name="/" handler="welcome-content" />
<filter-ref name="server-header" />
<filter-ref name="x-powered-by-header" />
<filter-ref name="Access-Control-Allow-Origin" />
<filter-ref name="Access-Control-Allow-Methods" />
<filter-ref name="Access-Control-Allow-Headers" />
<filter-ref name="Access-Control-Allow-Credentials" />
<filter-ref name="Access-Control-Max-Age" />
</host>
</server>
<servlet-container name="default">
<jsp-config />
<websockets />
</servlet-container>
<handlers>
<file name="welcome-content" path="${jboss.home.dir}/welcome-content" />
</handlers>
<filters>
<response-header name="server-header" header-name="Server"
header-value="WildFly/10" />
<response-header name="x-powered-by-header"
header-name="X-Powered-By" header-value="Undertow/1" />
<response-header name="Access-Control-Allow-Origin"
header-name="Access-Control-Allow-Origin" header-value="*" />
<response-header name="Access-Control-Allow-Methods"
header-name="Access-Control-Allow-Methods" header-value="GET, POST, OPTIONS, PUT" />
<response-header name="Access-Control-Allow-Headers"
header-name="Access-Control-Allow-Headers" header-value="accept, authorization, content-type, x-requested-with" />
<response-header name="Access-Control-Allow-Credentials"
header-name="Access-Control-Allow-Credentials" header-value="true" />
<response-header name="Access-Control-Max-Age"
header-name="Access-Control-Max-Age" header-value="1" />
</filters>
</subsystem>
重新启动服务器后,我的网络浏览器保留了相同的请求标头,没有Access-Control-Allow-Origin
的痕迹:
Request Headers :
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:fr-FR,fr;q=0.8,en;q=0.6,en-US;q=0.4
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Connection:keep-alive
...
有什么建议吗?