我有以下代码使用javascript SDK实现cloudsearch域。
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="aws-sdk-2.172.0.js"></script>
<script type="text/javascript">
console.log(AWS);
AWS.config.apiVersions = {
cloudsearch: 'latest',
};
var csd = new AWS.CloudSearchDomain({endpoint: 'search-mydomain-xxxxxxxxxxxxx.us-west-2.cloudsearch.amazonaws.com',region:'us-west-2'});
var params = {query:'test'};
csd.search(params, function (err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});
</script>
</head>
<body>
</body>
</html>
但它在控制台中显示错误
对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,'http://example.com'原产地不允许进入。响应具有HTTP状态代码400。
我需要在代码和aws帐户中更改什么?
CloudSearch不支持跨源资源共享(CORS)请求,因此您无法直接向CloudSearch域执行异步请求。当我遇到这个问题时,我最终做的是创建一个API网关来处理请求,并为此添加了CORS支持。因此,API网关接受请求,设置必要的标头以启用CORS,然后将请求中继到您的CloudSearch域。
以下是一些可能有助于设置此内容的链接: