简体   繁体   English

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

[英]AWS-SDK (lightsail API) vue.js CORS issues

I am trying to build a small vue.js app that uses aws-sdk to get all the Lightsial instances. 我正在尝试构建一个使用aws-sdk来获取所有Lightsial实例的小型vue.js应用程序。 However, I keep getting this error. 但是,我一直收到此错误。

: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.

Here is my vue component script 这是我的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 }); } } 

I am also using aws-amplify-vue in this app for user authentication 我也在此应用程序中使用aws-amplify-vue进行用户身份验证

Based on my research, it should be a CORS problem. 根据我的研究,应该是CORS问题。 Here is a similar thread in which some workaround are mentioned. 这是一个类似的线程,其中提到了一些解决方法。 Could your go to check if it works in your scenario? 您可以检查一下是否适合您的情况吗?

Why does my JavaScript get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not? 为什么Postman没有,我的JavaScript为什么会出现“请求资源上没有'Access-Control-Allow-Origin'标头”错误?

It simply because current release of < aws-sdk-js > does not allows CORS for Lightsail API. 仅仅是因为当前的< aws-sdk-js >版本不允许CORS for Lightsail API。

The official document informs that clearly. 官方文件清楚地说明了这一点。

EDIT: A completely wrong answer written by another user has been deleted after I added a comment to that. 编辑:我添加评论后,另一个用户写的完全错误的答案已被删除。 So I removed the words "Right answer" from my answer. 因此,我从答案中删除了“正确答案”一词。 Thanks for your quick action. 感谢您的快速操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM