13

我有一个托管在 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.

尝试访问 GAE url 的代码如下所示:

$.getJSON("http://---------appspot.com/Locations",function(result){
    for (i=0; i < result.length; i++)

任何帮助将不胜感激。

4

3 回答 3

11

您需要覆盖标准 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...
}

可以在此处找到一篇用示例解释 CORS 的精彩文章。

于 2012-12-10T12:30:38.303 回答
3

看起来您的请求在预检请求中提前失败,因为服务器在被请求时以 500 响应(它应该以 200 + 特定标头响应)OPTIONS <URL>

您可能想查看CORS 上的 HTML5rocks 教程,特别是向服务器添加 CORS 支持,其中解释了预检请求(该OPTIONS <url>请求,您的应用无法使用 200 多个必需的标头回复该请求)。

于 2012-04-28T20:58:29.170 回答
1

这是另一个解决方案(为我工作):

  1. 在您的 Java App Engine 项目中配置 CORS 支持:

将 mvn 放入您的 pom.xml 文件中:

<!-- CORS Support for Jetty -->
<dependency>
    <artifactId>jetty-servlets</artifactId>
    <groupId>org.eclipse.jetty</groupId>
    <version>9.2.22.v20170606</version>
</dependency>

或者下载 jar 文件:jetty-servlets.jar,把它放在你的 WEB-INF/lib

  1. 配置您的 web.xml 文件:

    <filter> <filter-name>cross-origin</filter-name> <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class> <init-param> <param-name>allowedOrigins</param-name> <param-value>*</param-value> </init-param> <init-param> <param-name>allowedMethods</param-name> <param-value>GET,POST,HEAD</param-value> </init-param> <init-param> <param-name>allowedHeaders</param-name> <param-value>X-Requested-With,Content-Type,Accept,Origin</param-value> </init-param> </filter> <filter-mapping> <filter-name>cross-origin</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

也许您需要使用自定义 URL 更改allowedOrigins字段值。

就是这样,构建和快乐编码。

有关附加信息: 如何将 Access-Control-Allow-Origin 添加到码头服务器

于 2017-08-24T16:02:10.037 回答