【发布时间】:2020-07-18 21:20:50
【问题描述】:
我对 Node.js、react 等并不精通,遇到以下问题:
我有一个在提交时从网页上的表单调用的函数,看起来像这样(出于隐私目的,我省略了一些细节):
class Email extends React.Component{
constructor(props){
super(props);
this.state = {
subject: "",
message: ""
};
}
handleUpdateSubjectLine(evt){
this.setState({ subject: evt.target.value});
}
handleUpdateMessageBox(evt){
this.setState({ message: evt.target.value});
}
async handle_email(evt){
var sgMail = require('@sendgrid/mail');
var subject = this.state.subject;
var message = this.state.message;
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
console.log("subject txt: " + subject);
console.log("msg txt: " + message);
var msg = {
to: 'OMMITED',
from: 'OMMITED',
subject: subject,
text: message,
};
sgMail.send(msg).catch(err =>{
console.log(err);
});
console.log("Message Allegedly Sent!");
}
当检查我的收件箱中由上述表格生成的测试电子邮件时,我不仅没有看到任何电子邮件,网络浏览器还会给出以下消息:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.sendgrid.com/v3/mail/send. (Reason: CORS header ‘Access-Control-Allow-Origin’ does not match ‘https://sendgrid.api-docs.io’).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.sendgrid.com/v3/mail/send. (Reason: CORS request did not succeed).
我从npm 安装了cors 和@sendgrid/mail,尽管看到其他人在YouTube 上实现它,但我似乎无法弄清楚如何将上述内容应用于此api 调用。
谢谢!
【问题讨论】:
-
您使用的是快递吗?如果是这样,您是否添加了这一行 "app.use(cors())" ?
-
你对
process.env.SENDGRID_API_KEY有哪些引用?这看起来不对。 -
@terrymorse ninja 在发布之前错过的编辑。
-
@ppichier 应用未定义
-
你是从浏览器运行,还是从节点服务器运行?