我在页面中有以下JavaScript代码.当进行ajax调用时,我可以看到浏览器inspect/debugger部分抛出net :: ERR_EMPTY_RESPONSE错误.它在localhost环境中工作正常,但在生产中抛出错误.
在客户端代码中,
<script>
$(document).ready(function(){
$("#identityLinks a").on("click", function(){
value = $(this).attr("id");
if(value != "")
{
$.ajax({
url: "publish/updateUsersData.php",
type: "POST",
data: {receiverId: value},
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType: "json",
success: function(data) {
//alert(data["result"]);
console.log(data["result"]);
},
error: function(xhr, textStatus, errorThrown) {
//alert(xhr +" "+ textStatus +" "+errorThrown);
console.log(xhr +" "+ textStatus);
}
});
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
在服务器端代码(PHP)中,我在updateUsersData.php中有以下代码:
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 1996 00:00:00 GMT');
header('Content-type: application/json; charset=UTF-8');
if(isset($_POST["receiverId"]))
{
$receiver_id = trim($_POST["receiverId"]);
$arr …Run Code Online (Sandbox Code Playgroud) 我有以下代码使用sendBeacon方法发送异步HTTP请求,
var data = {
name: 'test',
uniqueId: Math.random()
};
var blob = new Blob([JSON.stringify(data)], {type : 'application/json'});
navigator.sendBeacon('http://example.in/data/post', blob);
Run Code Online (Sandbox Code Playgroud)
这段代码已经很好地工作了很长时间.目前,由于Chrome https://bugs.chromium.org/p/chromium/issues/detail?id=490015中的安全问题,我们在"导航器"上看到错误" 无法执行'sendBeacon':sendBeacon()类型不是CORS安全的MIME类型的Blob不允许通过实验.有关详细信息,请参阅http://crbug.com/490015. "
是否有任何解决方法通过使用相同的sendBeacon API修改请求标头来发送JSON数据,直到问题得到解决?对于依赖此API的站点,在修复之前继续使用它将非常有用.关于使用XHR发布数据的建议没有用.
我将 JS timeonsite 库timeonsitetracker.js 集成到网页中,如文档中所示,
<script type="text/javascript">
var Tos;
(function(d, s, id, file) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s);
js.id = id;
js.onload = function() {
var config = {
trackBy: 'seconds',
developerMode: true
};
if(TimeOnSiteTracker) {
Tos = new TimeOnSiteTracker(config);
}
};
js.src = file;fjs.parentNode.insertBefore(js, fjs);
} (document, 'script', 'TimeOnSiteTracker', '//cdn.jsdelivr.net/gh/saleemkce/timeonsite@1.1.0/timeonsitetracker.min.js'));
</script>
Run Code Online (Sandbox Code Playgroud)
调用Tos对象后,
Tos.getTimeOnPage();
{
TOSId: 13650383319214852
TOSSessionKey: "6606159448467693493359"
TOSUserId: "anonymous"
URL: "https://localhost/index.html"
currentTime: "2020-07-11 16:25:17.908"
entryTime: "2020-07-11 16:24:36.911"
timeOnPage: 41
timeOnPageByDuration: …Run Code Online (Sandbox Code Playgroud)