我被Apigee的CORS支持難住了。我設置了一個新的代理,並確保勾選「爲您的API啓用直接瀏覽器訪問 - 允許通過CORS從瀏覽器發出直接請求。」框。Apigee選項404
看來,CORS正在爲正常的GET請求工作,但是沒有找到飛行前OPTIONS請求並且返回了404。我發現這個answer,但無法解決我的問題,因爲它看起來像一個不同的問題也許?
我想回答的主要問題是如何爲所有請求設置Access-Control-Allow-Origin = *?即使OPTIONS請求?
代理端點
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
<Description/>
<Flows>
<Flow name="Forecast">
<Description/>
<Request/>
<Response/>
<Condition>(proxy.pathsuffix MatchesPath "/forecast") and (request.verb = "GET")</Condition>
</Flow>
</Flows>
<PreFlow name="PreFlow">
<Request/>
<Response/>
</PreFlow>
<HTTPProxyConnection>
<BasePath>/v1/weather</BasePath>
<VirtualHost>default</VirtualHost>
<VirtualHost>secure</VirtualHost>
</HTTPProxyConnection>
<RouteRule name="default">
<TargetEndpoint>default</TargetEndpoint>
</RouteRule>
<PostFlow name="PostFlow">
<Request/>
<Response/>
</PostFlow>
</ProxyEndpoint>
目標端點
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="default">
<Description/>
<Flows>
<Flow name="OptionsCORS">
<Description/>
<Request/>
<Response>
<Step>
<Name>CrossOriginResourceSharing</Name>
</Step>
</Response>
<Condition>request.verb equals "OPTIONS"</Condition>
</Flow>
</Flows>
<PreFlow name="PreFlow">
<Request/>
<Response>
<Step>
<Name>CrossOriginResourceSharing</Name>
</Step>
</Response>
</PreFlow>
<HTTPTargetConnection>
<URL>https://home.nest.com/api/0.1/weather</URL>
</HTTPTargetConnection>
<PostFlow name="PostFlow">
<Request/>
<Response/>
</PostFlow>
</TargetEndpoint>
添加CORS文件
<AssignMessage async="false" continueOnError="false" enabled="true" name="CrossOriginResourceSharing">
<DisplayName>Add CORS</DisplayName>
<FaultRules/>
<Properties/>
<Add>
<Headers>
<Header name="Access-Control-Allow-Origin">*</Header>
<Header name="Access-Control-Allow-Headers">origin, x-requested-with, accept</Header>
<Header name="Access-Control-Max-Age">3628800</Header>
<Header name="Access-Control-Allow-Methods">GET, PUT, POST, DELETE, OPTIONS</Header>
</Headers>
</Add>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>
萬一它helps-以下是當仁不讓的請求時,我得到的錯誤。我使用的是Chrome,並且有一個AngularJS應用程序。我已經能夠使用cURL語句複製問題 (curl -H「Origin:localhost」--verbose http://*********-prod.apigee.net/v1/天氣預報/ 12345 -X選項)
{
"url": "/api/0.1/weather/forecast/73013",
"message": "404 Not Found"
}
謝謝!
有一個關於CORS預檢要求在Apigee一個文檔http://apigee.com/docs/api-services/content/adding -cors支持的API代理 –