4

我正在使用内部访问我的 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 烧瓶服务发出 AJAX POST 请求时出现了同样的错误,我通过在烧瓶中添加以下代码来修复它,

@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 操作正常工作?

4

4 回答 4

1

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

于 2015-09-26T06:42:43.023 回答
1

In my test,I set the wildcards(*) in origin tag, and I reproduced your issue, then I tried to specify the URL in allow-origins tag to development environment as http://localhost:8801 instead of (*), and it posted through.

Here are my code snippets, for your information:

Police: <inbound> <cors allow-credentials="false"> <allowed-origins> <origin>http://localhost:8801</origin> </allowed-origins> <allowed-methods> <method>*</method> </allowed-methods> <allowed-headers> <header>*</header> </allowed-headers> </cors> <base /> </inbound>

JS code is the example shown on API management portal:

$(function() { $.ajax({ url: "https://garytest.azure-api.net/testpost/", beforeSend: function(xhrObj){ // Request headers xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","{subscription key}"); }, type: "POST", // Request body data: {name:'gary',sex:'male'}, }) .done(function(data) { alert("success"); }) .fail(function() { alert("error"); }); });

Flask service is simple: @app.route('/post',methods=['POST']) def post(): name = request.form['name'] sex = request.form['sex'] return "post name: "+name+", sex:"+sex

Please try to specify origin, and test again.

于 2015-09-28T08:37:03.933 回答
0

使用此配置临时忽略 cors 检查

<cors allow-credentials="false">
        <allowed-origins>
            <!-- Localhost useful for development 
            <origin>http://localhost:4200</origin>-->
            <origin>*</origin>
        </allowed-origins>
        <allowed-headers>
            <!-- Examples below show Azure Mobile Services headers -->
            <header>*</header>
        </allowed-headers>
        <expose-headers>
            <!-- Examples below show Azure Mobile Services headers -->
            <header>*</header>
        </expose-headers>
    </cors>
于 2020-09-11T10:05:28.930 回答
0

在 API 管理中添加以下策略 -

<inbound>
    <cors>
        <allowed-origins>
            <origin>*</origin>
        </allowed-origins>
        <allowed-methods preflight-result-max-age="300">
            <method>*</method>
        </allowed-methods>
        <allowed-headers>
            <header>*</header>
        </allowed-headers>
        <expose-headers>
            <header>*</header>
        </expose-headers>
    </cors>
</inbound>

关键解决方案-删除-入站标签下的基本标签并保存。

于 2021-11-16T11:17:21.483 回答