![](/img/trans.png)
[英]AWS API Gateway No 'Access-Control-Allow-Origin' header is present
[英]Error 'Access-Control-Allow-Origin' using AWS API Gateway
我正在嘗試通過 AWS API Gateway 從我的客戶端(javascript/瀏覽器)向我的 API(python/flask)發出$ajax
請求(POST)。 但是,我收到一個錯誤: No 'Access-Control-Allow-Origin'
。
當我從postman
請求我的 API 時,我沒有問題。
我無法在客戶端的 ajax 中啟用選項jsonp
,因為它根本不起作用。
因此,我嘗試在 API Gateway 上啟用 cors,但我的路由是http proxy integration
(方法 ANY),因此根據 AWS Docs,我必須在應用程序本身中啟用 Cors。 所以,我做到了,但這並沒有解決問題。
在我的 python/flask 應用程序中,我添加了這個:
@app.after_request
def add_headers(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
return response
但它仍然不起作用。
有什么建議嗎?
嘗試將有效來源設置為Access-Control-Allow-Origin
標頭,而不是使用'*'
。
response.headers.add('Access-Control-Allow-Origin', request.headers.origin)
response.headers.add('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
對於大多數標頭,值
'*'
僅算作沒有憑據的請求(沒有HTTP
cookie 或HTTP
身份驗證信息的請求)的特殊通配符值。請參閱Access-Control-Allow-Origin 、 Access-Control-Allow-Headers 、 Access-Control-Allow-Methods等。
根據CORS 和緩存,您可能還必須添加以下標頭。
response.headers.add('Vary', 'Origin')
此外,您可能必須在使用$.ajax()
提交請求時定義xhrFields.withCredentials: true
。
$.ajax({
url: a_cross_domain_url,
xhrFields: {
withCredentials: true
}
...
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.