XMLHttpRequest 跨域网上有很多例子。
response.setHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
response.addHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With");
response.addHeader("Access-Control-Max-Age", "30");
//这样基本上可以了。 Access-Control-Allow-Origin是设置允许访问的域名, * 表示都可以访问。
关于XMLHttpRequest 的跨域可以看看 http://drops.wooyun.org/tips/188
我们用的springMVC做的接口。不支持XMLHttpRequest跨域时的预检方法OPTIONS。需要配置下web.xml->spring DispatcherServlet。具体信息请参考这个 http://stackoverflow.com/questions/23103832/spring-mvc-does-not-handle-requestmethod-options
很简单
<!-- 配置spring DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>dispatchOptionsRequest</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
只是加了个
<init-param>
<param-name>dispatchOptionsRequest</param-name>
<param-value>true</param-value>
</init-param>