Skip to main content
 首页 » 编程设计

jquery之Azure API 管理中的 CORS 问题

2025年05月04日43lautakyan007

我正在使用内部访问我的 python Flask Web 服务的 Azure API 管理。 Azure API 适用于 GET 操作。对于 POST,当我进行 jquery AJAX 调用时,请求被转换为 OPTIONS 并显示以下错误

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://domain.com' is therefore not allowed access. The response had HTTP status code 500. 

我为 Azure API 添加了以下策略,

<policies> 
    <inbound> 
        <cors> 
            <allowed-origins> 
                <origin>*</origin> 
            </allowed-origins> 
            <allowed-methods> 
                <method>*</method> 
            </allowed-methods> 
            <allowed-headers> 
                <header>*</header> 
            </allowed-headers> 
        </cors> 
        <base /> 
    </inbound> 
    <outbound> 
        <base /> 
    </outbound> 
</policies> 

但我仍然面临同样的问题。

当我直接向我的 python flask 服务发出 AJAX POST 请求时出现了同样的错误,我通过在 flask 中添加以下代码来修复它,

@app.after_request 
def after_request(response): 
  response.headers.add('Access-Control-Allow-Origin', '*') 
  response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization') 
  response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE') 
  return response 

我应该在 Azure API 管理中更改什么以使 POST 操作正常工作?

请您参考如下方法:

上一个版本中存在 CORS 错误,现已解决。