Shijin TR
Shijin TR

Reputation: 7768

Amazon cloudsearch not working

I have the following code for implementing cloudsearch domain using javascript SDK.

<!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>

But it shows an error in the console

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access. The response had HTTP status code 400.

What i need to change in the code and aws account?

Upvotes: 1

Views: 1237

Answers (1)

dmbaughman
dmbaughman

Reputation: 608

CloudSearch does not support cross-origin resource sharing (CORS) requests, so you cannot do an async request directly to the CloudSearch domain. What I ended up doing when I came across this issue was create an API Gateway to handle the request, and added CORS support to that. So the API Gateway accepts the request, sets up the necessary headers to enable CORS, and then relays the request to your CloudSearch domain.

Here are a couple of links that might be helpful in setting this up:

Upvotes: 2

Related Questions