Azure API管理> CORS和POST [英] Azure API Management > CORS and POST
问题描述
我正在使用Azure API Management为第三方提供干净的界面以进行集成。
I'm using Azure API Management to deliver a clean interface to third parties for integration purposes.
我想用JSON对象进行POST以创建此对象在后端。这在门户网站中可用的测试控制台中工作正常,但是当我尝试从网页上执行简单的客户端脚本时它不起作用:
I want do a POST with a JSON object to create this object in the backend. This works fine in the test console available in the portal site, but it doesn't work when I try to do a simple client script from a web page:
$.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方法返回以下标题:
Setting the contentType header to 'application/json' forces the browser to perform an OPTIONS call first. My WebAPI project is setup to enable CORS and I have tested this. My WebAPI project returns the following headers for the OPTIONS method:
Access-Control-Allow-Head ... content-type
Access-Control-Allow- Orig ... *
Access-Control-Allow-Head... content-type Access-Control-Allow-Orig... *
但是,如果我尝试使用Azure Management API调用此操作,我会获得OPTIONS方法的200状态,但没有其他标题存在。我尝试了很多政策配置,这是我最近的尝试:
However, if I try to call this operation by using the Azure Management API, I get a 200 status for the OPTIONS method, but no other headers are present. I've tried many policy-configurations, this was my latest attempt:
<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>
我缺少什么让这项工作?
What am I missing to make this work?
推荐答案
ajax请求中的标题应为content-type而不是contentType
The header in the ajax request should be 'content-type' rather than 'contentType'
这篇关于Azure API管理> CORS和POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!