JavaScript-重定向前的日志记录

JavaScript-重定向前的日志记录,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正试图在重定向之前发送一个ajax调用(用于日志记录)。实际上,重定向是一种下载,它正确地设置了八位字节流和配置。因此,并非所有浏览器(尤其是chrome浏览器)都会调用ajax调用。在其他像IE这样的公司。我怎样才能确保调用被确实执行 代码如下: $(function() { $('#download-link-a').click(function() { remoteLog ('Clicked on download', '<?php echo $userna

我正试图在重定向之前发送一个ajax调用(用于日志记录)。实际上,重定向是一种下载,它正确地设置了八位字节流和配置。因此,并非所有浏览器(尤其是chrome浏览器)都会调用ajax调用。在其他像IE这样的公司。我怎样才能确保调用被确实执行

代码如下:

$(function() {
    $('#download-link-a').click(function() {
        remoteLog ('Clicked on download', '<?php echo $username; ?>' );
        location.href = "<?php echo $secure_link; ?>";
        return false;
    });
});
function remoteLog (arg, key) {
    var file = '/files/remoteLog.php';
    $.post(file, {text: arg, key: key});
}
$(函数(){
$('#download-link-a')。单击(函数(){
remoteLog('点击下载','');
location.href=“”;
返回false;
});
});
函数remoteLog(参数,键){
var file='/files/remoteLog.php';
$.post(文件,{text:arg,key:key});
}
$(函数(){
$('download-link-a')。单击(函数(e){
e、 预防默认值();
remoteLog('点击下载','');
});
});
函数remoteLog(参数,键){
var file='/files/remoteLog.php';
var jqPost=$.post(文件,{text:arg,key:key})
.always(function(){//或.done()也可以使用
location.href=“”;
返回false;
});
}

将重定向移动到回调函数:

function remoteLog (arg, key) {
    var file = '/files/remoteLog.php';
    $.post(file, {text: arg, key: key})
      .always(function(){location.href = "<?php echo $secure_link; ?>";});
}
函数远程日志(参数,键){
var file='/files/remoteLog.php';
$.post(文件,{text:arg,key:key})
.always(函数(){location.href=”“;});
}

只有在成功发布
POST
后,才需要使用回调函数来调用重定向。我建议您使用
$.ajax()
而不是
$.post()
,因为它更易于定制

以下是如何使用适当的回调函数将
$.post()
转换为
$.ajax()
等效程序:

$(function() {
    $('#download-link-a').click(function() {
        remoteLog ('Clicked on download', '<?php echo $username; ?>' );
        return false;
    });
});

function remoteLog (arg, key) {

    var fnSuccess = function() {
        location.href = "<?php echo $secure_link; ?>";
    };

    var fnFail = function() {
        alert('POST failed. Do not redirect!');
    };

    $.ajax({
      type: 'POST',
      url: '/files/remoteLog.php',
      success: fnSuccess,
      error: fnFail,
      data: {text: arg, key: key},
      dataType: 'json',
      async:true
    });
}
$(函数(){
$('#download-link-a')。单击(函数(){
remoteLog('点击下载','');
返回false;
});
});
函数remoteLog(参数,键){
var fnsucture=function(){
location.href=“”;
};
var fnFail=函数(){
警报('POST失败。不要重定向!');
};
$.ajax({
键入:“POST”,
url:“/files/remoteLog.php”,
成功:成功,
错误:fnFail,
数据:{text:arg,key:key},
数据类型:“json”,
异步:true
});
}

更多阅读:

我将使用
onbeforeuload
和同步ajax调用

$("#some-link").click(function(evt){
     this.outlink = $(this).attr("href");
     this.outtext = "Some text";
});

window.onbeforeunload = function(){
  data = {text:this.outtext,outlink:this.outlink};
  $.ajax({
    url:url,
    type:"POST",
    data:data,
    async:false
  });
};
事实上,您唯一的问题是ajax调用的
async
部分

$("#some-link").click(function(evt){
     this.outlink = $(this).attr("href");
     this.outtext = "Some text";
});

window.onbeforeunload = function(){
  data = {text:this.outtext,outlink:this.outlink};
  $.ajax({
    url:url,
    type:"POST",
    data:data,
    async:false
  });
};