首页 > 解决方案 > 是否可以通过 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');
    });
});    
});

标签: javascriptfirebasevue.jsgoogle-cloud-functions

解决方案


GET您可以使用?&符号 在(在这种情况下)请求中向服务器发送多个值。

?符号表示以下字符串序列是服务器可以读取的值。符号是值之间的分隔符
。 每个值都必须是一个键值对,例如.&
key=value

考虑以下fetch请求。

fetch('https://example.com/user?first_name=foo&last_name=bar')

在此示例中,我们将first_name和发送last_namehttps://example.com/userURL。在那里,服务器能够读取这些值。

在 PHP 中,它就像下面的示例。

$first_name = $_GET['first_name']; // foo
$last_name = $_GET['last_name']; // bar

我希望这能够帮到你。


推荐阅读