AWS-SDK(lightsail API)vue.js CORS问题

时间:2018-11-23 04:11:26

标签: javascript vue.js aws-sdk amazon-lightsail

我正在尝试构建一个使用aws-sdk来获取所有Lightsial实例的小型vue.js应用。但是,我一直收到此错误。

:8081/#/:1 Access to XMLHttpRequest at 'https://lightsail.us-west-2.amazonaws.com/' from origin 'http://localhost:8081' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

这是我的vue组件脚本

import { AmplifyEventBus } from 'aws-amplify-vue'
import { components } from 'aws-amplify-vue'
import AWS from 'aws-sdk'
import Lightsail from 'aws-sdk/clients/lightsail'
import Auth from '@aws-amplify/auth';

import awsconfig from '../aws-exports';


export default {
components: {
    AWS, Lightsail
  },
  data() {
    return {
     
    }
  },
  mounted() {

  var myCredentials = 
    {
      accessKeyId : '***************',
      secretAccessKey : '****************'
    }

  AWS.config.update({
    credentials: myCredentials, region: 'us-west-2'
  });

    var lightsail = new AWS.Lightsail();
    lightsail.getInstances(function (err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
    });




  }

}

我还在此应用中使用aws-amplify-vue进行用户身份验证

2 个答案:

答案 0 :(得分:0)

根据我的研究,应该是CORS问题。这是一个类似的线程,其中提到了一些解决方法。您可以检查一下是否适合您的情况吗?

Why does my JavaScript get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?

答案 1 :(得分:0)

这仅仅是因为当前版本的<aws-sdk-js>确实不允许对Lightsail API进行CORS

official document清楚地表明了这一点。

编辑:在我添加评论之后,另一个用户写的完全错误的答案已被删除。因此,我从答案中删除了“正确答案”一词。感谢your的迅速行动。