javascript - 是否可以通过 fetch 获取多个数据?
问题描述
是否可以通过 fetch 获取多个数据?在我的示例中,我得到“inputDest”值(电子邮件)和“a”值(名称)。所以我的目标是获取“inputDest”和“a”值并将其发送到电子邮件。
const inputDest = document.querySelector('input[name=dest]');
const a=document.querySelector('input[name=a]');
document.querySelector('button[name=send-mail]').addEventListener('click', function(){
fetch('https://us-central1-nastava-3ae81.cloudfunctions.net/sendMail?dest='+inputDest.value+'sendMail?a='+a.value)
.then(response => response.text())
.then(text => {
console.log(text);
});
}, false);
如果我留下这样的代码,我当然会收到一个错误:错误:无法发送邮件 - 所有收件人都被拒绝:553-5.1.3 收件人地址不是 553 5.1.3 有效的 RFC-5321 地址。m189sm881135ioa.17 - gsmtp
所以,在我的功能里面基本上是这样的
exports.sendMail = functions.https.onRequest((req, res) => {
cors(req, res, () => {
const dest = req.query.dest;
const a=req.query.a;
const mailOptions = {
from: '<something@gmail.com>',
to: dest,
subject: a,
html: `<p style="font-size: 16px;">Pickle </p> `
};
return transporter.sendMail(mailOptions, (erro, info) => {
if(erro){
return res.send(erro.toString());
}
return res.send('Sended');
});
});
});
解决方案
GET
您可以使用?
和&
符号 在(在这种情况下)请求中向服务器发送多个值。
该?
符号表示以下字符串序列是服务器可以读取的值。符号是值之间的分隔符
。
每个值都必须是一个键值对,例如.&
key=value
考虑以下fetch
请求。
fetch('https://example.com/user?first_name=foo&last_name=bar')
在此示例中,我们将first_name
和发送last_name
到https://example.com/user
URL。在那里,服务器能够读取这些值。
在 PHP 中,它就像下面的示例。
$first_name = $_GET['first_name']; // foo
$last_name = $_GET['last_name']; // bar
我希望这能够帮到你。
推荐阅读
- datepicker - 我们如何在 Ionic 中设置最大日期
- spring-boot - 我应该明确验证 Keycloak 令牌还是由 Keycloak 适配器完成?
- python - 在 kaggle 控制台中看不到完整的错误日志
- linux - 如何在 yocto build 中将 libgif 添加为包
- spring-boot - 在docker容器内的外部tomcat上运行spring boot时出错
- python - python中两个不同数据帧之间的数值差异
- c# - 如何在 C# 中通过模式动态验证 JSON
- cmd - 无法在 cmd 中找到特定的可执行文件
- ios - watchOS 6 WKInterfaceImage 在释放时崩溃
- java - 谷歌云调试器线程“无法继续”