2015-09-25 46 views
1

我正在使用内部访问我的python Flask Web服务的Azure API管理。 Azure API适用于GET操作。对于POST,当我提出的jQuery AJAX调用,请求转换成选项和下面的错误显示出来Azure API管理中的CORS问题

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> 

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

同样的错误出现了,当我直接让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操作工作?

+0

我最近也注意到了我的Azure api中的CORS故障,截至今天。这也是你最近的问题吗? – Haymaker87

+0

我们也注意到了它们,从昨天开始。 – Brian

+0

是的。最近的问题,浪费了一整天的时间。没有预料到来自微软 – Selva

回答

1

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

0

在我的测试,我在origin标签设置通配符(*),我复制你的问题,然后我试图在指定的URL允许-起源标签开发环境http://localhost:8801代替(*),它张贴通过。

这里是我的代码片段,供您参考:

警方: <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代码是API管理门户所示的例子:

$(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"); }); });

烧瓶服务很简单: @app.route('/post',methods=['POST']) def post(): name = request.form['name'] sex = request.form['sex'] return "post name: "+name+", sex:"+sex

请尝试指定原点,然后再次测试。