我已经在CORS / Lambda / AWS API网关配置上阅读了很多内容,包括AWS的设置帮助: http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html 这篇伟大的文章: https://serverless.com/framework/docs/providers/aws/events/apigateway/
然而,CORS仍在击败我的API Gateway / Lambda功能,我无法弄明白。会爱一些帮助。
它的核心似乎是API网关将请求代理到Lambda,您需要设置" Access-Control-Allow-Origin":" *" Lambda函数中的标题除了在API网关中启用CORS之外,我已经完成了。我收到200响应,可以看到从我的Lambda函数正确发回的标题。但是,Chrome / FF仍然给我一个CORS错误。知道为什么吗?
以下是我的请求标题:
tck = interp2d(np.linspace(0, 1200, data.shape[1]),
np.linspace(0, 1200, data.shape[0]),
data,
fill_value=255)
data = tck(range(1200), range(1200))
data = np.ma.MaskedArray(data, data == 255)
200响应标题:
Host: myawshost.execute-api.us-west-2.amazonaws.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: application/json, text/javascript
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: application/x-www-form-urlencoded
Origin: null
DNT: 1
Connection: keep-alive
这一切看起来都很好而且成功吗?但是,我从Lambda中得不到JSON,并在控制台中收到此错误消息:
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 74
Content-Type: application/json
Date: Fri, 23 Dec 2016 08:35:02 GMT
...
但是"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://myawshost.execute-api.us-west-2.amazonaws.com/prod/view?id=272. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). (unknown)"
并没有丢失!我可以在回复中看到它。
有什么想法?
答案 0 :(得分:2)
我也花了很多时间对此进行研究,而对我来说起作用的是在lambda处理程序(Python)中返回了以下内容:
return {
'statusCode': status_code,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': status_message if status_code == 200 else 'Failure'
}
答案 1 :(得分:1)