App Engine端点启用CORS

问题描述:

我在标准环境v2端点类中有一些方法。当从Web客户端调用它们时,首先会调用OPTIONS方法来检查是否启用了CORS。 回应是:App Engine端点启用CORS

HTTP/1.1 200 OK 
X-Cloud-Trace-Context: 2a246f2e7f7ddbbf2afeaa71629da259;o=1 
Date: Wed, 12 Jul 2017 13:47:20 GMT 
Expires: Wed, 12 Jul 2017 13:47:20 GMT 
Cache-Control: private, max-age=0 
X-Content-Type-Options: nosniff 
X-Frame-Options: SAMEORIGIN 
X-XSS-Protection: 1; mode=block 
Content-Length: 0 
Server: GSE 
Content-Type: text/html; charset=UTF-8 
Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,36,35" 

什么是这里缺少的是Access-Control-Allow-Origin: *响应头。有什么办法可以启用它吗?

+0

你在用什么语言? – saiyr

+0

端点是用Java编写的。 – piotrpo

答案中描述的问题是我的错(真是一个惊喜)。 对于其他在GAE上配置CORS时出现问题的问题: 如果CORS设置为开箱即用。但对于得到正确的响应,你还必须有正确的请求:

Origin: null 
Access-Control-Request-Method: GET 

方法必须是下列之一:"HEAD", "DELETE", "GET", "PATCH", "POST", "PUT" 地实际上可以是任何字符串 - 你会找回来的响应。 “null”是翻译为“*”的特殊关键字。 这是负责头生成的代码(从GAE源):

public static void allowOrigin(HttpServletRequest request, HttpServletResponse response) { 
    String origin = request.getHeader(Headers.ORIGIN); 
    // The Origin spec (http://tools.ietf.org/html/draft-abarth-origin-09) allows for the Origin 
    // http header value to be "null". This is for cases where a request doesn't have a valid 
    // origin; for example, issuing a CORS request from a local file:// rather than a website. In 
    // these cases, we'd like to enable CORS to facilitate testing; the mechanism for doing so is 
    // to set the Access-Control-Allow-Origin header to '*'. 
    origin = NULL_ORIGIN.equals(origin) ? "*" : origin; 
    response.setHeader(Headers.ACCESS_CONTROL_ALLOW_ORIGIN, origin); 
    } 

我的第二个问题是由于错误的不稳定构建合并摇篮文件。