【发布时间】:2021-04-08 16:13:10
【问题描述】:
我正在使用 cors npm 包来解决 cors 问题,但无济于事。这以前可以工作,但是当我切换到我的开发环境时,我不断收到此错误:
Access to fetch at 'https://carside.firebaseapp.com/sendsmstoconsumer' from origin 'https://carside.web.app' 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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
这就是我的函数代码的设置方式:
const functions = require('firebase-functions')
const express = require('express')
const app = express()
const accountSid = 'SECRET'
const authToken = 'SECRET'
const client = require('twilio')(accountSid, authToken)
const cors = require('cors')
// Enable cors for everything
// TODO: Re-enable cors at a later date
// src: https://www.npmjs.com/package/cors
app.use(cors())
app.post('/sendsmsverificationmessage', cors() ,(request, response) => {
client.verify.services('SECRET')
.verifications
.create({to: request.body.phone, channel: 'sms'})
.then(verification => {
response.send("Your verification code has been sent to your phone!" )
});
})
exports.app = functions.https.onRequest(app)
【问题讨论】:
-
app.use(cors())使所有页面都可以通过 cors 访问 -
是的,应该是我仍然不断收到无法访问的错误
-
我现在收到此错误
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.
标签: javascript node.js firebase google-cloud-functions cors