我正在尝试在聚合物应用程序中的iron-ajax客户端上设置api密钥以使用Amazon Web Services的API网关 endpoints . 在服务器端(API网关),我在cors上启用了通配符,还启用了x-api-key作为标头并授予了凭据 . 当我运行客户端时,我总是在Chrome中获得相同的响应:
对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头 . 因此,不允许来源“http:// localhost:8080”访问 . 响应的HTTP状态代码为403.网络响应为{“message”:“Forbidden”}
我尝试了以下代码,以多种方式改变了事情,但我什么也没得到 .
<iron-ajax
id="requestOpensource"
url='https://myservice.execute-api.eu-west-1.amazonaws.com/development/myservice'
method='GET'
headers = "{{ajaxParams}}"
handle-as="json"
content-type="application/json"
on-response="handleResponse"
with-credentials="true"
on-error="handleErrorResponse">
</iron-ajax>
</template>
<script>
Polymer({
is: 'my-view1',
properties: {
response: {
type: Object
},
apikey: {
type: String,
value: '12345'
},
ajaxParams: {
type: String,
computed: 'processParams(apikey)'
},
headers: {
type: Object,
computed: 'getHeaders(customHeader)'
},
customHeader: {
value: '12345'
}
},
getHeaders: function(customHeader) {
return {'X-API-Key': customHeader}
},
processParams: function(apikey) {
return {
key: apikey
}
},
ready: function () {
debugger
this.$.requestOpensource.headers['x-api-key'] = "12345"
//this.$.requestOpensource.headers['Access-Control-Allow-Origin'] = "*"
this.$.requestOpensource.generateRequest()
}
</script>
我也使用chrome插件来启用cors,但它对我的客户来说不是一个好的解决方案(我也得到了同样的响应) . 我认为问题是没有正确设置 Headers 上的数据 . 但我读了多个论坛,我不知道接下来该做什么 .
非常感谢您的回复!