Azure API Management > CORS 和 POST



我正在使用Azure API管理为第三方提供一个干净的接口,用于集成目的。

我想用JSON对象进行POST,在后端创建这个对象。这在门户网站的测试控制台中运行良好,但当我尝试从网页上执行一个简单的客户端脚本时,它就不起作用了:

$.ajax({
    url: 'https://.azure-api.net/api/samplerequest/create?' + $.param(params),
    type: 'POST',
    data: JSON.stringify(sampleRequest),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data, par2, par3) {
        $("#txtResult").val(JSON.stringify(data));
    }
});

将contentType标头设置为"application/json"会强制浏览器首先执行OPTIONS调用。我的WebAPI项目是为了启用CORS而设置的,我已经对此进行了测试。我的WebAPI项目返回OPTIONS方法的以下标头:

访问控制允许标头。。。内容类型访问控制允许原始…*

但是,如果我尝试使用Azure Management API调用此操作,则OPTIONS方法的状态为200,但不存在其他头。我尝试过很多策略配置,这是我最近的一次尝试:

<policies>
    <inbound>
        <base />
        <cors>
            <allowed-origins>
                <origin>*</origin>
                <!-- allow any -->
            </allowed-origins>
            <allowed-methods>
                <method>POST</method>
                <method>OPTIONS</method>
            </allowed-methods>
            <allowed-headers>
                <header>contentType</header>
            </allowed-headers>
        </cors>
    </inbound>
    <outbound>
        <base />
    </outbound>
</policies>

我缺少什么来完成这项工作?

ajax请求中的头应该是"content-type",而不是"content-type"

最新更新