我正在尝试使用React将一些数据发送到Node。这是我的React代码:
sendMail(e) {
e.preventDefault();
// fetch('/https://uczsieapp-mailer.herokuapp.com/', {
var name = document.getElementById('name').value;
var contactReason = document.getElementById('contactReason').value;
var email = document.getElementById('email').value;
var additionalInfo = document.getElementById('additionalInfo').value;
var body = {
name: name,
contactReason: contactReason,
email: email,
additionalInfo: additionalInfo,
};
body = JSON.stringify(body);
console.log(body);
fetch('http://localhost:4000/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
content: body,
}).then(r => console.log(r)).catch(e => console.log(e));
}
这是我的Node代码:
var cors = require('cors');
var app = express();
app.use(cors());
var a = '=';
router.post('/', (req, res, next) => {
console.log('mailing');
console.log(a);
a += '=';
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: "smtp.gmail.com", // hostname
auth: {
user: 'someuser@gmail.com',
pass: 'testgmail'
}
});
console.log(req.body.content);
let mailOptions = {
from: `${req.body.name} ${req.body.email}`, // sender address
to: 'alexander.ironside@mygeorgian.ca', // list of receivers
subject: 'Email from UczSieApp contact form', // Subject line
text: 'Hello world ', // plaintext body
html: `
<h4>Imie: ${req.body.name}</h4>
<h4>Email: ${req.body.email}</h4>
<h4>Powod kontaktu: ${req.body.contactReason}</h4>
<p>Wiadomosc: ${req.body.additionalInfo}</p>
`
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
}
);
如您所见,我正在使用cors module,它应该可以处理所有cors
问题。
但这还不够。当我摆脱传递给headers
的{{1}}属性时,调用已完成,但未发送任何数据。添加fetch()
时,我会很开心
跨域请求被阻止:“同源起源”策略禁止读取http://localhost:4000/处的远程资源。 (原因:CORS标头“ Access-Control-Allow-Origin”缺失)。
再次出错。
我如何解决它?我想念什么?
我在编写代码时使用了这个答案:https://stackoverflow.com/a/42965820/7055769
答案 0 :(得分:1)
您可以尝试以下操作,这应该可以解决问题:
app.use(function (req, res, next) {
//set headers to allow cross origin requestt
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
答案 1 :(得分:1)
事实证明,CORS仅允许某些特定的内容类型。
Content-Type标头的唯一允许值为:
origin参数指定可以访问资源的URI。浏览器必须执行此操作。对于没有凭据的请求,服务器可以将“ *”指定为通配符,从而允许任何源访问资源。
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
<input type="text">
<br/>
<button onClick="testEmpty()">Check</button>
<p/>
<span id="result"></span>
答案 2 :(得分:0)
好吧,这有很多原因不起作用。我将这个答案留给未来的开发人员来减轻他们的痛苦。
这是在客户端使用的方法:
$.ajax({
type: "POST",
url: 'http://localhost:4000/',
data: body,
success: data=>{console.log(data)},
dataType: 'json',
});
这是我的整个服务器端代码:
var express = require('express');
var router = express.Router();
var cors = require('cors');
var app = express();
app.use(cors());
app.options('*', cors());
var a = '=';
router.post('/', (req, res, next) => {
console.log('mailing');
console.log(a);
a += '=';
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: "smtp.gmail.com", // hostname
auth: {
user: 'sqtableknights@gmail.com',
pass: 'testgmail'
}
});
console.log(req.body);
let mailOptions = {
from: `${req.body.name} ${req.body.email}`, // sender address
to: 'alexander.ironside@mygeorgian.ca', // list of receivers
subject: 'Email from UczSieApp contact form', // Subject line
text: 'Hello world ', // plaintext body
html: `
<h4>Imie: ${req.body.name}</h4>
<h4>Email: ${req.body.email}</h4>
<h4>Powod kontaktu: ${req.body.contactReason}</h4>
<p>Wiadomosc: ${req.body.additionalInfo}</p>
`
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
}
);
module.exports = router;
因此,在我的情况下,我必须添加以下内容:
app.use(cors());
app.options('*', cors());
不知道为什么,但是没有app.options('*', cors());
参数就中断了。如果有人对此有一个更好的答案,和/或发现任何安全漏洞,请发表评论并告诉我。再次感谢大家的帮助!