Google App Engine和CORS
我在GAE上托管了一个简单的应用程序(java servlet)。 该应用程序返回json数据。 我在servlet中设置了以下标题信息:
resp.setContentType("application/json"); resp.setHeader("Access-Control-Allow-Origin", "*"); resp.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); resp.setHeader("Access-Control-Allow-Credentials", "true");
当我直接在应用引擎上访问url时,这是标题信息:
Request Method:GET Status Code:200 OK Request Headersview source Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Cache-Control:max-age=0 Connection:keep-alive Host:---------.appspot.com User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19 Response Headersview source Access-Control-Allow-Credentials:true Access-Control-Allow-Methods:GET, POST, OPTIONS Access-Control-Allow-Origin:* Cache-Control:private Content-Encoding:gzip Content-Length:340 Content-Type:application/json; charset=ISO-8859-1 Date:Sat, 28 Apr 2012 19:14:58 GMT Server:Google Frontend Vary:Accept-Encoding
但是,当我尝试从其他域访问该URL时,我得到以下响应:
Request Method:OPTIONS Status Code:500 Internal Server Error Request Headersview source Accept:*/* Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Access-Control-Request-Headers:origin, x-requested-with, accept Access-Control-Request-Method:GET Connection:keep-alive Host:----------.appspot.com Origin:http://--------------.com Referer:http://-------------.com/map/ User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19 Response Headersview source Content-Length:466 Content-Type:text/html; charset=UTF-8 Date:Sat, 28 Apr 2012 19:15:14 GMT Server:Google Frontend
这是确切的错误:
XMLHttpRequest cannot load http://----------.appspot.com/Locations. Origin http://-------------.com is not allowed by Access-Control-Allow-Origin.
尝试访问GAEurl的代码如下所示:
$.getJSON("http://---------appspot.com/Locations",function(result){ for (i=0; i < result.length; i++)
任何帮助都会非常感激。
您需要覆盖标准的HttpServlet.doOptions()方法以支持正确的飞行前请求处理 。
@Override protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // pre-flight request processing resp.setHeader("Access-Control-Allow-Origin", "*"); resp.setHeader("Access-Control-Allow-Methods", SUPPORTED_METHODS); resp.setHeader("Access-Control-Allow-Headers", SUPPORTED_HEADERS); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setHeader("Access-Control-Allow-Origin", "*"); resp.setContentType("application/json"); // implementation... }
看起来您的请求在预检请求中提前失败,因为服务器在请求OPTIONS
时会响应500(它应该响应200 +特定标头)。
您可能想要查看CORS上的HTML5rocks教程 ,特别是向服务器添加CORS支持,其中解释了预检请求( OPTIONS
请求,您的应用无法使用200 +所需的标头回复)。
这是另一种解决方案(为我工作):
- 在Java App Engine项目中配置CORS支持:
用mvn放入你的pom.xml文件:
jetty-servlets org.eclipse.jetty 9.2.22.v20170606
或者下载jar文件: jetty-servlets.jar ,将它放在WEB-INF / lib中
-
配置web.xml文件:
cross-origin org.eclipse.jetty.servlets.CrossOriginFilter allowedOrigins * allowedMethods GET,POST,HEAD allowedHeaders X-Requested-With,Content-Type,Accept,Origin cross-origin /*
也许您需要使用自定义URL更改allowedOrigins字段值。
多数民众赞成,建立和快乐的编码。
有关 附加 信息: 如何将Access-Control-Allow-Origin添加到jetty服务器
- GWT-No’Access-Control-Allow-Origin’标头出现在请求的资源上
- 使用application / json contentType的AJAX POST调用获取“no’Access-Control-Allow-Origin’标头”错误
- Spring Global CORS配置不起作用,但控制器级配置不起作用
- Spring CorsFilter似乎仍然没有在预检请求上收到401
- 无法在javascript中向java Web服务(泽西岛)发出CORS POST请求?
- 预检的响应具有无效的HTTP状态代码401 – Spring
- 使用Spring Framework为OPTIONS请求启用CORS
- GWT HTTP请求响应代码0,CORS正在工作
- 在ContainerResponseFilter中获得响应(JAX-RS 2)