繁体   English   中英

上传到 Cloudinary 时如何修复 CORS 阻止错误

[英]How do I fix CORS blocked error when uploading to Cloudinary

我正在制作一个个人网站,需要用户通过签名上传文件将文件上传到 Cloudinary。 我的代码是

const cloudinary = require('cloudinary/lib/cloudinary').v2;

        cloudinary.config({ 
            cloud_name: 'cloud_name', 
            api_key: 'api_key', 
            api_secret: 'api_secret' 
        });


        cloudinary.uploader.upload(image,{public_id: id}, function(error, result) { });

每当我运行 cloudinary.uploader.upload 时,我都会在浏览器中收到此错误

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.cloudinary.com/v1_1/cloud_name/image/upload. (Reason: header ‘user-agent’ is not allowed according to header ‘Access-Control-Allow-Headers’ from CORS preflight response).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.cloudinary.com/v1_1/cloud_name/image/upload. (Reason: CORS request did not succeed).

同样的事情发生在我身上,然后我意识到我忘记将 API 密钥和秘密环境变量添加到我部署的代码中。 只要有未捕获的错误,似乎就会弹出 CORS 错误。 如果您已经在服务器中启用了 CORS 并且“CORS”看起来一切正常,那么这可能是其他一些错误。

编辑:补充一点,在我输入正确的 api 键后,我仍然遇到同样的问题。 事实证明,我使用的是 firebase 函数,而 multer 很奇怪。 我从 multer 变成了 busboy,一切都很顺利。 见链接: https://stackoverflow.com/a/47603055/15560288

暂无
暂无

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

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