javascript - 使用 href 和 javascript 抛出错误在 facebook 上共享页面
问题描述
我有一个从实时数据库加载其内容的页面,它是一个事件页面,因此图像、标题等根据查询参数而有所不同。在我尝试通过 jquery 添加 og 元标记信息但它不起作用之前,所以我改变了我的方法以使用带有节点的云函数,使用 express,节点返回一个填充了所有元标记的页面但是当我单击按钮时与该 facebook url 共享页面 window.open("https://www.facebook.com/sharer/sharer.php?u=https://apptcc-6f556.firebaseapp.com/evento"+url); 我收到一个错误,url 只是 location.search 函数中的一个字符串。无论如何,如果我手动将事件的 url 复制到 facebook 上,一切都会正常进行。我给你看图片和代码
这是 index.ejs 负责人的代码
<meta property="og:image" content="<%= evento.caminho %>">
<meta property="og:title" content="<%= evento.titulo %>">
<meta property="og:description" content="<%= evento.descricao %>">
这是我的云函数 index.js 中的代码
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require("express");
const app = express();
admin.initializeApp();
function getEvento(pais,categoria,id){
return adminRef.child(`eventos/${pais}/${categoria}/${id}`).once("value")
.then(evento=>evento.val());
}
app.get("/evento",(req,res)=>{
const evento = {
titulo:"teste",
id:"-MCxaVd4JwRcbdK174Sn",
descricao:"dasd",
caminho:"https://firebasestorage.googleapis.com/v0/b/apptcc-
6f556.appspot.com/o/eventos%2FBrazil%2FMG%2FEspinosa%2FVLxS4OQN4CaTK89A7f6853wEX7P2%2F-M7ZG-
5Yg9u41UC_Pda-?alt=media&token=e7f28393-6c0a-4095-8c46-5485ed82824c"
}
return res.render("index.ejs",{evento});
getEvento(req.query.pais,req.query.categoria,req.query.id)
.then(evento=>{
return res.render("index.ejs",{evento})
})
.catch(error=>{
console.log(error);
res.send(error);
})
})
exports.app = functions.https.onRequest(app)
顺便说一句,当我创建静态对象时,它可以工作,但是当它来自数据库时,它不会。并且在我的云功能控制台上也得到这个错误,这真的很奇怪,因为它说是空的,但是当我在 html 的正文中打印时它工作正常,当我打开谷歌控制台并检查头部元素时,即使它说它为空,有关事件的所有信息都设置在 og 元标记中
解决方案
没关系,我发现了错误,发生的情况是,当我写我的 url 时,我使用“&”字符来分隔参数,而发生的是 facebook 的 api 或其他任何东西,无法理解 & 字符,所以我刚刚使用了javascript函数encodeURIComponent并且它有效
推荐阅读
- reactjs - Devextreme 作为共享点框架的外部库
- javascript - 正则表达式与 /.../
- java - 错误:无法找到或加载主类 org.openjdk.jmh.runner.ForkedMain
- php - php preg_replace iOS 撇号
- react-native - 如何在 React Native 中查看 API 数据更新,例如消息应用程序中收到的新消息?
- excel - 在 VBA Excel 中将带有十六进制命令的字符串发送到串行端口
- azure-devops - 管理多个 VSTS 变量
- java - 使用 javax.print 将纯文本文件打印到 PDF 打印机会导致一个空文件
- html - 如何在html5中仅使用css在容器内绘制具有四个div的矩形?
- c# - WPF 主应用程序动态调用 DLL...在返回主应用程序之前需要来自 DLL 的消息传递