- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于我的生活,我无法弄清楚发生了什么。我整天都在寻找答案,但在任何地方都找不到。我正在练习编写一个函数,通过电子邮件将废弃的表单发送给我。
这是我为托管表单的本地主机运行的 index.js 文件
const inputSelector = document.getElementById("name");
const fieldSelector = document.querySelectorAll('.formfield');
let formData = {};
fieldSelector.forEach(field =>{
field.addEventListener('input', (e) =>{
let formField = e.target.id;
formData[formField] = field.value;
});
})
window.addEventListener('beforeunload', () =>{
fetch('http://localhost:8080/', {
method:'post',
headers:{
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: formData
})
})
})
请求发送到的函数是一个包含以下代码的云函数:
exports.testFetch = async (req, res) =>{
console.log(req.method);
if(req.method === 'OPTIONS'){
console.log('method is option')
res.set('Access-Control-Allow-Origin', "*")
res.set('Access-Control-Allow-Methods', 'GET, POST');
res.set('Access-Control-Allow-Headers', 'Content-Type');
}
else{
console.log('full body: ', req.body);
console.log('message: ', req.body.message);
}
res.send('response')
}
现在,每当我填写表单字段然后从该页面浏览到另一个页面时,它都能完美运行,云函数 console.logs 表单字段。但是,当我只是通过单击 X 关闭页面时,云功能只有 console.log 'OPTIONS' 和 'method is option'。看起来它只发送发布请求的选项部分。我整个下午都在寻找解决方案,但到处都找不到。 super 令人沮丧。当我只是将获取功能添加到按钮然后按下按钮时,它也能完美运行。只是关闭浏览器,它似乎不起作用并卡在 OPTIONS 中。如果您有任何提示,请告诉我!
最佳答案
正如 Endless 的回答所讨论的,sendBeacon 是一个选项。但问题是您的提取请求未使用 keepalive flag .
Normally, when a document is unloaded, all associated network requestsare aborted. But the keepalive option tells the browser to perform therequest in the background, even after it leaves the page. So thisoption is essential for our request to succeed.
window.addEventListener('beforeunload', () => {
fetch('http://localhost:8080/', {
method:'post',
headers:{
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: formData
}),
keepalive: true // this is important!
})
})
关于javascript - 使用 'beforeunload'/'unload' eventlistener 关闭浏览器后使用 Fetch 发送 POST 请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63157089/
我在 Chrome 和 Safari 上试过了,这两个浏览器都不行。当我刷新页面时,Chrome 中只有一个警告窗口如下所示。如果仅使用 evt.preventDefault(),则该函数不起作用。不
我的代码: //hold window open on form change window.onbeforeunload = function(e) { if(formChanges > 0
以下代码始终显示相同的消息:此页面要求您确认等。是否可以更改此文本以显示其他内容? $(window).bind('beforeunload', function(){ }); 是否可以查询用户是真的
我有一小段代码: $(window).bind('beforeunload', function() { $.ajax({ async: false, type: 'POST',
我想知道是否可以在 beforeunload 事件上向数据库发送查询。 $(window).on('beforeunload',function() { console.log('before
import React, { useState, useEffect } from "react"; import "./styles.css"; export default function A
我在移动浏览器上工作了大约 3 个月,现在我遇到了一个无聊的问题 beforeunload和 onload .在两种情况下,它们不能清楚地工作。当我关闭选项卡时,浏览器经常调用它们。但是,当用户通过一
第 1 页包含动态表单,第 2 页是对提交数据的审核。我有以下限制: 提交第 1 页时没有 beforeunload 警告; beforeunload 刷新页面 1 时发出警告; 从第 2 页发出 h
我已经使用 window.beforeunload 功能禁用了页面重定向/重新加载,但是一旦页面中发生表单提交,我想重定向到下一页,而不显示 window.beforeunload 警报。我怎样才能做
我有一个表单,当某些元素获得焦点时它会发生变化。 我希望在离开页面时收到警告。所以我保留了一个标志,告诉我这些元素何时发生变化。 问题在于,“此页面正在要求您确认是否要离开”警报始终提示; funct
我一直在思考应该使用哪一个:beforeunload或onbeforeunload它们似乎都在做非常相似的事情,但具有不同的浏览器兼容性。 一些背景: 我有一个表格。在页面加载时,我序列化表单并将其保
当用户单击按钮时,我使用 JqueryUI 自定义确认框。 这是脚本, function exit() { $("#dialog-confirm").dialog({
我正在尝试找出如何限制表单上的 beforeunload 命令。我已经能够弄清楚如何取消绑定(bind) beforeunload 命令,然后当我离开这个有效的表单时。但是,无论表单是否更改,每次都会
在我的应用程序中,我有一个评论框,我想在其中显示一条消息,表明它尚未保存。在函数 warnOpenEditor 中,我添加了 window.onbeforeunload 事件,但它不会触发。附上TS和
我有一个简单的函数,可以在发出新页面请求时显示旋转器叠加层。 $(window).on("beforeunload", function() { $('#nav-spinner').show();
我有一个主干应用程序,我想在导航到未保存的更改之前提醒用户。我有一个事件要做: $(window).bind('beforeunload', function() { var status =
我正在将 JSF 与omnifaces 和cdi 一起使用。在 xhtml 页面中,我使用了一些类似于(下面的 jsf 页面有更多详细信息) 的 js window.onbeforeunload =
我在我的页面上使用了 beforeunload 事件,这样当用户试图关闭我的网站/标签时,我会提示用户他们不应该离开网站,除非填写所有表格。但问题是我的页面有一个丰富的表单,在每个部分表单填写后重新加
我正在页面中检测表单更改,所以我已经处理了 $('#element').change(function(){ metaDataChange = true; }); 这样当用户离开页面并且在表单的输入中
我明白用自定义对话框替换 beforeunload 对话框是不可能的,如果我们需要为用户设置自定义消息,我们必须返回一个字符串我们的 beforeunload 处理程序: {Custom messag
我是一名优秀的程序员,十分优秀!